And when is the `inplace` argument misleading?
Mar 27, 2023
Several methods for the Pandas
DataFrame support an
inplace argument. You
can find quite contradicting advice around it online. Some describe it as "good
Pandas docs says
"its use is discouraged".
inplace=Truekeyword has been a controversial topic for many years. It is generally seen (at least by several pandas maintainers and educators) as bad practice and often unnecessary, but at the same time it is also widely used, partly because of confusion around the impact of the keyword.
PDEP-8 / Motivation and Scope
Operations that re-arrange the rows of a
DataFrame can't be executed without
copying. Avoid the
inplace argument for these methods.
Pandas provides many different methods for a
DataFrame. Let's group them based
on the type of change. Then consider whether they can be executed without
creating a copy of the
|operation||example methods||possible without a copy|
|delete rows from the end||
|delete rows from arbitrary location||
|mutate the elements||
Note the difference between:
Removing rows from the end of a
DataFrame can happen without creating a copy:
pop operates default in place.
Removing rows from an arbitrary position re-arranges some of the remaining rows.
=> Here, a copy is needed. Methods like
dropna can't operate in place.
The problem is that there are several methods that
Examples for this include
The Pandas Enhancement Proposal,
PDEP-8, suggests the removal
inplace argument for such
In the PDEP, the Pandas methods that provide some inplace functionality are
grouped into 4 groups. Group 4 contains the methods that provide an
copy option, although they always need to create a copy. The PDEP suggests the
removal of these options.
Even if these
inplace options aren't deprecated currently, it's better to
avoid them for these "Group 4" methods.
DataFrameobject makes the code more confusing and prone to bugs 🐛.
Noneis confusing and hinders method chaining.
The "Group 4" methods where the usage of
inplace is possible but discouraged:
For methods that can happen with or without copying the
DataFrame object, the
inplace argument is OK and can bring performance gains. Examples are
Sourcery has introduced the
for the "Group 4" methods in the versions: