12

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.

2
  • 2
    I will always recommend using deep copy Commented Jul 18, 2018 at 0:00
  • pretty good answer here: stackoverflow.com/questions/38895768/… Commented Dec 3, 2020 at 3:47

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

3 Comments

dataframes are mutable, not immutable
@PaulH maybe op said DataFrame is an example of not immutable (=mutable) objects
@ DavidJohns: the way that's written is very ambiguous e.g. "If arg is not a bear (e.g. a a koala). Much clearer to say If it's a mutable object..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.