【问题标题】:Pandas: SettingWithCopyWarning trigger location熊猫:SettingWithCopyWarning 触发位置
【发布时间】:2015-02-23 21:34:26
【问题描述】:

使用SettingWithCopyWarning,有时它会将您指向模块中触发警告的确切代码行(例如here),有时则不会(例如here)。

没有遍历代码的每一行(如果您正在查看数百行代码,这听起来不太吸引人),有没有一种方法可以确定触发警告的代码行,假设警告没有返回那个信息?

我想知道这是否是警告中的一个错误,即返回警告而不查明触发它的特定代码。

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

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    通过将mode.chained_assignment 选项设置为raise 而不是warn,您可以让pandas 提高SettingWithCopyError 而不是SettingWithCopyWarning

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-25
      • 2017-12-23
      • 1970-01-01
      • 2017-05-07
      • 2016-11-03
      • 2019-11-06
      相关资源
      最近更新 更多