This post covers a common pandas behavior: DataFrames are mutable, so a function can modify the caller’s object even if you do not return it. The behavior is documented in the copy vs view guide and the DataFrame API.
Downloads at the end: go to Downloads.
At a glance
- A DataFrame is mutable; in-place ops modify the original object.
- The bug looks like “nothing was returned, but things changed.”
- Fix it by copying, returning, or making mutation explicit.
Minimal repro (runnable)
Run this as a plain script. The function changes raw, even if you ignore the return value.
| |
Expected output:
| |
Why it happens
DataFrame is mutable. When you pass it into a function, you pass a reference. Any in‑place operation (dropna(inplace=True), assignment to a column, rename(inplace=True)) changes that same object in memory. Pandas warns about this in the copy vs view section.
Safer patterns (pick one)
1) Return a new object
Make a copy and return the new DataFrame.
| |
2) Mutate on purpose
If you want in‑place behavior, make that explicit.
| |
3) Make intent visible in the name
Callers should know what the function does.
| |
Practical checklist
- Avoid
inplace=Trueunless you truly want to mutate the input. - If you mutate, name it
*_inplace. - If you return a new object, copy first.