pandas chained_assignment 警告异常处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47182183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:45:04  来源:igfitidea点击:

pandas chained_assignment warning exception handling

pythonpandasexception-handling

提问by HeXor

If you worked in python with pandas you already know the chained_assignment warning when working on slices of dataframes (as e.g. described here).

如果你在Python曾与Pandas你dataframes的切片工作的时候就已经知道chained_assignment警告(如例如描述这里)。

I found the option pandas.options.mode.chained_assignmentwhich can be set to

我找到了pandas.options.mode.chained_assignment可以设置为的选项

  • None, ignoring the warning
  • "warn", printing a warning message
  • "raise", raising an exception
  • None,忽略警告
  • "warn", 打印警告信息
  • "raise", 引发异常

compare with documentation

文档比较

I included a minimal example, triggering this warning within a try..except..elseblock for exception handling. I expect an exception to be triggered only with the setting pandas.options.mode.chained_assignment = "raise", as shown in Example 3 below.

我包含了一个最小示例,在try..except..else异常处理块中触发此警告。我希望仅使用设置触发异常pandas.options.mode.chained_assignment = "raise",如下面的示例 3 所示。

In this minimal example, the behaviour is as expected, so that Example 2, with pandas.options.mode.chained_assignment = "warn"only causes a warning message to be printed, but no exception is raised.

在这个最小的示例中,行为符合预期,因此示例 2 pandas.options.mode.chained_assignment = "warn"仅会导致打印警告消息,但不会引发异常。

However in a larger framework, I see an exception being raised even though the parameter is set to pandas.options.mode.chained_assignment = "warn", checked with a print before like in the minimal example (see Example 4)

然而,在一个更大的框架中,我看到一个异常被引发,即使参数被设置为pandas.options.mode.chained_assignment = "warn",就像在最小的例子中一样之前用打印检查过(参见示例 4)

Is there any other pandas parameter influencing the behavior of this warning message concerning exception raising?

是否有任何其他 Pandas 参数会影响此有关异常引发的警告消息的行为?



Here is a minimal example of code, setting/printing the pd.options.mode.chained_assignmentparameter and showing the behaviour in a try..catch..exceptblock.

这是一个最小的代码示例,设置/打印pd.options.mode.chained_assignment参数并在try..catch..except块中显示行为。

import pandas as pd

# set the chained_assignment option
pd.options.mode.chained_assignment = "raise" # raises exception in case of warning
pd.options.mode.chained_assignment = "warn"  # prints warning in case of warning, no exception is raised
pd.options.mode.chained_assignment = None    # no warning message and no exception is raised

print "pd.options.mode.chained_assignment :", pd.options.mode.chained_assignment

# create a default pandas dataframe with two columns A,B
df = pd.DataFrame({"A" : [0, 1, 2], "B" : [3, 4, 5]})

print df

# exctract a slice of the given pandas dataframe
df2 = df[df["A"] > 0]

# exception handling
try :
    # try to modify the slice, triggering the pandas warning
    df2["C"] = 2
except :
    print "EXCEPTION RAISED"
else :
    print "NO EXCEPTION"

print df2


Example 1 Setting pd.options.mode.chained_assignment = Noneresults in the following output (no warning, no exception)

示例 1 设置pd.options.mode.chained_assignment = None结果如下输出(无警告,无异常)

pd.options.mode.chained_assignment : None
   A  B
0  0  3
1  1  4
2  2  5
NO EXCEPTION
   A  B  C
1  1  4  2
2  2  5  2


Example 2 Setting pd.options.mode.chained_assignment = "warn"results in the following output (a warning is printed, but no exception)

示例 2 设置pd.options.mode.chained_assignment = "warn"结果如下输出(打印警告,但无异常)

pd.options.mode.chained_assignment : warn
   A  B
0  0  3
1  1  4
2  2  5
NO EXCEPTION
C:\Users\my.name\my\directory\test.py:14: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
  df2["C"] = 2
   A  B  C
1  1  4  2
2  2  5  2


Example 3 Setting pd.options.mode.chained_assignment = "raise"results in the following output (an exception is raised)

示例 3 设置pd.options.mode.chained_assignment = "raise"导致以下输出(引发异常)

pd.options.mode.chained_assignment : raise
A  B
0  0  3
1  1  4
2  2  5
EXCEPTION RAISED
   A  B  C
1  1  4  2
2  2  5  2


Example 4 This is what I see in a larger framework with exactly the same test code. I do not set the chained pd.options.mode.chained_assignmentparameter explictly, but I see it's set to "warn", even though an exception is raised

示例 4 这是我在具有完全相同测试代码的更大框架中看到的。我没有pd.options.mode.chained_assignment明确设置链式参数,但我看到它被设置为"warn",即使引发了异常

pd.options.mode.chained_assignment warn
   A  B
0  0  3
1  1  4
2  2  5
EXCEPTION RAISED
   A  B  C
1  1  4  2
2  2  5  2

采纳答案by HeXor

After a long search, the "bad guy" was found. Another developer included the following lines in his module

经过长时间的搜索,找到了“坏人”。另一位开发人员在他的模块中包含以下几行

import warnings
warnings.filterwarnings('error')

This turns warnings into exceptions. For more details see warnings package documentation

这会将警告变成异常。有关更多详细信息,请参阅警告包文档

Hence my warnings were treated as exceptions, although the pandas option was set to "warn"

因此,我的警告被视为例外,尽管Pandas选项设置为“警告”