Amelio Vazquez-Reina Amelio Vazquez-Reina - 3 months ago 29
Python Question

SettingWithCopyWarning, even when using loc (?)

I get

SettingWithCopyWarning
errors in cases where I would not expect them:

N.In <38>: # Column B does not exist yet
N.In <39>: df['B'] = df['A']/25
N.In <40>: df['B'] = df['A']/50

/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/indexing.py:389: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
self.obj[item] = s


and

N.In <41>: df.loc[:,'B'] = df['A']/50

/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/indexing.py:389: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
self.obj[item] = s


Why does it happen in case 1 and 2?

Answer

In case 1, df['A'] creates a copy of df. As explained by the Pandas documentation, this can lead to unexpected results when chaining, thus a warning is raised. Case 2 looks correct, but false positives are possible:

Warning: The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertantly reported.

To turn off SettingWithCopyWarning for a single dataframe, use

df.is_copy = False

To turn off chained assignment warnings altogether, use

options.mode.chained_assignment = None
Comments