Skip to main content
Back to Python Coding

Python Coding · Where pandas Lies to You

The View vs Copy Bug

The one idea: A "cleaning" step can run with no error, print "done," and change nothing at all — leaving your analysis built on the exact data you thought you had fixed. The tell is two square-bracket selections before an equals sign. After any cleaning step, check the data itself rather than trusting the message.
⚠️ The trap: df[mask]['col'] = value picks those rows and sets the column, so it updates my data. That feels right and it's wrong. Those two chained square-bracket selections (df[...][...]) land on a throwaway copy, so the original is left untouched and the "cleaning" does nothing — use df.loc[mask, 'col'] = value and re-check the condition afterward.

The most notorious trap in pandas is a "cleaning" step that runs without error, looks like it fixed the data, and changed absolutely nothing. It's the bug behind a thousand Stack Overflow questions and one cryptic message — SettingWithCopyWarning — and it shows up regularly in AI-generated notebooks. You don't need to be able to fix it. You need to recognize the shape of it on sight, because an analysis built on data that was never actually cleaned is an analysis of the wrong data.

The setup: two square brackets and an equals sign

Suppose a notebook wants to zero out negative revenue values (refund artifacts, say) before summing:

df[df['revenue'] < 0]['revenue'] = 0

Read it left to right and it seems fine: "take the rows with negative revenue, set their revenue to 0." It runs. And in most real situations, the original df is unchanged — the negative values are still there, and every downstream sum…

Keep reading: The View vs Copy Bug

Unlock the full Python Coding track — every lesson, the AI tutor, and full mock assessments.

  • Full lesson content
  • AI tutor for this section
  • Practice questions