简体   繁体   中英

How can I figure out and access pandas warning subclasses?

I'm trying to troubleshoot this widely discussed pandas warning .

In trying to pinpoint the location (though I'm also interested in how to do this generally), I want to set up a warnings.simplefilter to error on just the SettingWithCopyWarning.

I'm trying:

import warnings
warnings.simplefilter("error", SettingWithCopyWarning)

That doesn't work, because the interpretter doesn't know what SettingWithCopyWarning is. I assume it's a sublass of Warning created by pandas, but I'm not sure how to set up this filter properly (or how to access the class directly).

For additional color, the warnings (in recent versions of python or pandas, not sure where the change was made) DO tell you the location, but in this case it's happening in the pandas core code:

/path/to/my/virtualenv/lib/python2.7/site-packages/pandas/core/indexing.py:426: 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 caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

I need to see the whole traceback to figure out what pandas call was made that ultimately triggered this warning, hence my desire to get the error.

Thanks for helping figure out how to pinpoint this warning.

You need to specify its precise location :

import pandas as pd
import warnings
warnings.simplefilter("error", pd.core.common.SettingWithCopyWarning)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM