如何使用 warnings.simplefilter() 忽略 SettingWithCopyWarning? [英] How to ignore SettingWithCopyWarning using warnings.simplefilter()?

查看:238
本文介绍了如何使用 warnings.simplefilter() 忽略 SettingWithCopyWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我可以使用 warnings.simplefilter() 忽略或阻止将 SettingWithCopyWarning 打印到控制台吗?

详情:

我正在使用 Pandas 运行一些数据清理例程,这些例程使用

除了整理出错误来源之外,还有什么方法可以防止错误消息像我可以使用 FutureWarnings 一样打印到提示中,例如 warnings.simplefilter(action = "ignore", category = FutureWarning)?

解决方案

虽然我强烈建议解决这个问题,但可以通过从 pandas.core.common 导入来抑制警告.我在 GitHub 上找到了它的位置.

示例:

导入警告将熊猫导入为 pd从 pandas.core.common 导入 SettingWithCopyWarningwarnings.simplefilter(action="ignore", category=SettingWithCopyWarning)df = pd.DataFrame(dict(A=[1, 2, 3], B=[2, 3, 4]))df[df['A'] >2]['B'] = 5 # 链式赋值没有警告!

The question:

Can I ignore or prevent the SettingWithCopyWarning to be printed to the console using warnings.simplefilter()?

The details:

I'm running a few data cleaning routines using pandas, and those are executed in the simplest of ways using a batch file. One of the lines in my Python script triggers the SettingWithCopyWarning and is printed to the console. But it's also being echoed in the command prompt:

Aside from sorting out the source of the error, is there any way I can prevent the error message from being printed to the prompt like I can with FutureWarnings like warnings.simplefilter(action = "ignore", category = FutureWarning)?

解决方案

Though I would strongly advise to fix the issue, it is possible to suppress the warning by importing it from pandas.core.common. I found where it's located on GitHub.

Example:

import warnings

import pandas as pd
from pandas.core.common import SettingWithCopyWarning

warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)

df = pd.DataFrame(dict(A=[1, 2, 3], B=[2, 3, 4]))
df[df['A'] > 2]['B'] = 5  # No warnings for the chained assignment!

这篇关于如何使用 warnings.simplefilter() 忽略 SettingWithCopyWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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