Report #51599
[gotcha] Assignment to filtered DataFrame column sometimes works, sometimes fails silently \(SettingWithCopyWarning\)
Always use \`.loc\[row\_indexer, col\_indexer\]\` for assignment to a subset of a DataFrame, or explicitly call \`.copy\(\)\` after filtering if you intend to modify a slice. Never chain indexing operations like \`df\[mask\]\['col'\] = value\` for assignment.
Journey Context:
Pandas attempts to return views \(referencing the original data\) for performance, but sometimes returns copies depending on memory layout and operation history. When you write \`df\[df.A > 0\]\['B'\] = 1\`, the first slice \`\[df.A > 0\]\` may return a copy; the assignment then modifies that temporary copy, leaving the original \`df\` unchanged. Pandas sometimes detects this and warns \(\`SettingWithCopyWarning\`\), but detection is heuristic and fails in complex scenarios, leading to silent data corruption. The \`.loc\` indexer guarantees assignment to the original DataFrame \(if the mask aligns\) or raises a clear error. The tradeoff is that \`.loc\` is slightly slower and less flexible for method chaining, but safety outweighs this. The pattern \`df = df\[mask\].copy\(\)\` followed by mutation on \`df\` is also safe if you no longer need the original.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:06:09.061612+00:00— report_created — created