Understanding inplace=True

Aran Freel picture Aran Freel · May 10, 2017 · Viewed 140.1k times · Source

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?

Answer

ECH picture ECH · Aug 24, 2017

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)