如何找出并访问 pandas 警告子类? [英] How can I figure out and access pandas warning subclasses?

查看:30
本文介绍了如何找出并访问 pandas 警告子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决这个被广泛讨论的熊猫警告.

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

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.

我正在尝试:

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

这不起作用,因为解释器不知道 SettingWithCopyWarning 是什么.我假设它是由 pandas 创建的警告子类,但我不确定如何正确设置此过滤器(或如何直接访问该类).

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).

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

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

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

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)

这篇关于如何找出并访问 pandas 警告子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆