pandas :SettingWithCopyWarning触发器位置 [英] Pandas: SettingWithCopyWarning trigger location

查看:73
本文介绍了 pandas :SettingWithCopyWarning触发器位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于SettingWithCopyWarning,有时它会将您引向触发警告的模块中的确切代码行(例如在此).

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

推荐答案

通过将mode.chained_assignment选项设置为raise而不是warn,可以使熊猫举起SettingWithCopyError而不是SettingWithCopyWarning.

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

这篇关于 pandas :SettingWithCopyWarning触发器位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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