Fix Python – Understanding inplace=True in pandas

Question

Asked By – Aran Freel

In the pandas library many times there is an option to change the object inplace such as with the following statement…

df.dropna(axis='index', how='all', inplace=True)

I am curious what is being returned as well as how the object is handled when inplace=True is passed vs. when inplace=False.

Are all operations modifying self when inplace=True? And when inplace=False is a new object created immediately such as new_df = self and then new_df is returned?


If you are trying to close a question where someone should use inplace=True and hasn’t, consider replace() method not working on Pandas DataFrame instead.

Now we will see solution for issue: Understanding inplace=True in pandas


Answer

When inplace=True is passed, the data is renamed in place (it returns nothing), so you’d use:

df.an_operation(inplace=True)

When inplace=False is passed (this is the default value, so isn’t necessary), performs the operation and returns a copy of the object, so you’d use:

df = df.an_operation(inplace=False) 

This question is answered By – Ed Harrod

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0