8

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.

aydow
  • 3,456
  • 2
  • 21
  • 37
WhiteDillPickle
  • 135
  • 1
  • 4
  • 8
  • 2
    I will always recommend using deep copy – BENY Jul 18 '18 at 00:00
  • pretty good answer here: https://stackoverflow.com/questions/38895768/python-pandas-dataframe-is-it-pass-by-value-or-pass-by-reference – JohnE Dec 03 '20 at 03:47

1 Answers1

15

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.

smci
  • 29,564
  • 18
  • 109
  • 144
aydow
  • 3,456
  • 2
  • 21
  • 37