我如何找出并访问 pandas 警告子类?

How can I figure out and access pandas warning subclasses?

我正在尝试解决问题 this widely discussed pandas warning

在尝试查明位置时(尽管我也对一般如何执行此操作感兴趣),我想设置一个 warnings.simplefilter 以仅在 SettingWithCopyWarning 上出错。

我正在尝试:

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

那行不通,因为解释器不知道 SettingWithCopyWarning 是什么。我假设它是由 pandas 创建的 Warning 子类,但我不确定如何正确设置此过滤器(或如何直接访问 class)。

对于其他颜色,警告(在 python 或 pandas 的最新版本中,不确定更改的位置)确实会告诉您位置,但在这种情况下,它发生在pandas核心代码:

/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

我需要查看整个回溯以找出最终触发此警告的 pandas 调用,因此我希望得到错误。

感谢您帮助找出如何查明此警告。

您需要指定它的精确位置:

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