Suppose a Pandas DataFrame is passed to a function as an argument. Then, does Python implicitly copy that DataFrame or is the actual DataFrame being passed in?
Hence, if I perform operations on the DataFrame within the function, will I be changing the original (because the references are still intact)?
I just want to know whether or not I should make a Deep Copy of my DataFrame before passing it into a function and operating on it.
If a function parameter is a mutable object (e.g. a DataFrame
), then any changes you make in the function will be applied to the object.
E.g.
In [200]: df = pd.DataFrame({1:[1,2,3]})
In [201]: df
Out[201]:
1
0 1
1 2
2 3
In [202]: def f(frame):
...: frame['new'] = 'a'
...:
In [203]: f(df)
In [204]: df
Out[204]:
1 new
0 1 a
1 2 a
2 3 a
See this article for a good explanation on how Python passes function parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With