Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SettingWithCopyWarning, even when using loc (?) [duplicate]

Tags:

python

pandas

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?

like image 763
Amelio Vazquez-Reina Avatar asked May 15 '14 20:05

Amelio Vazquez-Reina


People also ask

What is SettingWithCopyWarning?

A SettingWithCopyWarning warns the user of a potential bug and should never be ignored even if the program runs as expected. The warning arises when a line of code both gets an item and sets an item. Pandas does not assure whether the get item returns a view or a copy of the dataframe.


2 Answers

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
like image 66
kxsong Avatar answered Oct 21 '22 23:10

kxsong


Another solution that should suppress the warning:

df = df.copy()
df['B'] = df['A']/25
df['B'] = df['A']/50
like image 44
Charlie Haley Avatar answered Oct 21 '22 23:10

Charlie Haley