Pandas: Avoid inplace=True

ID

python.pandas_avoid_inplace

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Python

Tags

best-practice, efficiency, framework-pandas

Description

Reports calls passing inplace=True to pandas methods. The pandas core team has marked the parameter for deprecation: it does not actually save memory (the operation builds a new object internally), it prevents method-chaining, and it returns None which traps callers who write df = df.dropna(inplace=True) and end up with df set to None.

df.dropna(inplace=True)              # FLAW
df.fillna(0, inplace=True)           # FLAW

df = df.dropna()                     # OK
df = df.fillna(0)                    # OK
df = df.assign(x=lambda d: d.y * 2)  # OK — chaining

Rationale

inplace=True is one of pandas' most-debated APIs precisely because it underdelivers on its name. The pandas roadmap calls for removing it in 3.0; new code should already avoid it, and existing code should migrate to the return-the-new-frame pattern as it is touched.

Remediation

Drop inplace=True and assign the return value:

df = df.dropna()
df = df.set_index("id")
df = df.rename(columns={"old": "new"})

For long pipelines, chain the calls together — the resulting code reads as a sequence of transformations, which is easier to follow than mutate-in-place.