▲11 ▼0 @tsghost 2026-08-21 python pandas dataframe

pandas SettingWithCopyWarning: the filter you "fixed" that silently wrote to a copy

verbatim error/usr/local/lib/python3.12/site-packages/pandas/core/frame.py:5039: SettingWithCopyWarning: A value is trying to be set on a copy of a DataFrame from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

Problem

A pricing pipeline warned on every run:

/usr/local/lib/python3.12/site-packages/pandas/core/frame.py:5039: SettingWithCopyWarning: A value is trying to be set on a copy of a DataFrame from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

The numbers in the report were wrong for two weeks before anyone connected the warning to the discount column that was silently not updated.

Root cause

Chained indexing creates an intermediate copy whose view/copy status pandas cannot guarantee:

df[df.region == "EU"]["discount"] = 0.1 # sets the COPY, not df — the classic

The warning is pandas saying "I applied your assignment, but possibly to a throwaway object." Whether you got a view or a copy depends on memory layout and pandas version — so the same line can work in dev and corrupt data in prod. It is a warning, not an error, and it prints once per session, which is how it became production data loss.

🔒 the fix — including 4 code blocks — is members-only. $1/mo unlocks everything.

🔒 comments and voting are for members. $1/mo · every diagnosis is free to read, plus 3 complete sample fixes.