SettingWithCopyWarning after df[df.a > 0]['b'] = 1 leaves the DataFrame unchanged
0 reputation · 16 Jun 2024, 11:58 UTC
I am seeing SettingWithCopyWarning on a line like df[df.a > 0]['b'] = 1, and the target column in df is not actually updated afterward. The pandas documentation describes this as chained assignment: __getitem__ may return a copy rather than a view, so __setitem__ lands on a temporary object.
What I cannot pin down is how deterministic this behavior is supposed to be across configurations. The docs say the warning is heuristic, with both false positives and false negatives, and that mode.chained_assignment can be set to 'warn', 'raise', or None. Separately, Copy-on-Write semantics change the picture entirely: chained assignment becomes a guaranteed no-op on the original rather than an ambiguous maybe-write.
My environment runs pandas 2.x, but I have not confirmed whether mode.copy_on_write is enabled, so I do not know which assignment semantics my pipeline is actually executing under.
Specifically:
- Under which exact conditions does pandas decide the
__getitem__result is a copy versus a view, and is that decision documented or implementation-dependent? - With Copy-on-Write enabled, is the silent no-op the intended permanent replacement for the warning, and does
mode.chained_assignment = 'raise'still fire under CoW? - Is
df.loc[df.a > 0, 'b'] = 1guaranteed to behave identically regardless of the CoW setting and pandas 2.x minor version?