Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: SettingWithCopyWarning trigger location

Tags:

python

pandas

With SettingWithCopyWarning, sometimes it refers you to the exact line of code in your module that triggered the warning (e.g. here) and other times it doesn't (e.g. here).

Short of going through each line of the code (doesn't sound too appealing if you're reviewing hundreds of lines of code), is there a way to pinpoint the line of code that triggered the warning assuming the warning does not return that information?

I wonder if this is a bug in the warning to return a warning without pinpointing the specific code that triggered it.

Warning (from warnings module):
  File "C:\Python34\lib\site-packages\pandas\core\indexing.py", line 415
    self.obj[item] = s
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Python 3.4, Pandas 0.15.0

like image 754
sedeh Avatar asked Dec 26 '14 16:12

sedeh


1 Answers

You can have pandas raise SettingWithCopyError instead of SettingWithCopyWarning by setting the mode.chained_assignment option to raise instead of warn.

import pandas as pd
pd.set_option('mode.chained_assignment', 'raise')

https://pandas.pydata.org/pandas-docs/stable/options.html#available-options

like image 155
Roy Hyunjin Han Avatar answered Oct 21 '22 11:10

Roy Hyunjin Han