如何抑制熊猫未来警告?

big*_*bug 92 suppress-warnings pandas

当我运行程序时,Pandas每次都给出如下所示的"未来警告".

D:\Python\lib\site-packages\pandas\core\frame.py:3581: FutureWarning: rename with inplace=True  will return None from pandas 0.11 onward
  " from pandas 0.11 onward", FutureWarning) 
Run Code Online (Sandbox Code Playgroud)

我得到了消息,但我只想阻止Pandas一次又一次地显示这样的消息,是否有任何buildin参数我可以设置让Pandas不会弹出'Future warning'?

bdi*_*nte 209

github上发现这个...

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)

  • 嘿,尽管添加了这些行,我还是收到了未来警告。 (29认同)
  • nb:把`warnings .... ignore` _before_'import pandas ...'放到`FutureWarning`被忽略. (26认同)
  • 这不适合我.我正在使用anaconda python3(2017年9月21日) (4认同)

小智 21

只需在开始代码之前放置此行即可。

import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)


Alo*_*yak 14

警告很烦人。正如其他答案中所述,您可以使用以下方法抑制它们:

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)

但是如果你想一一处理它们并且你正在管理一个更大的代码库,将很难找到导致警告的代码行。由于与错误不同的警告不带有代码回溯。为了跟踪错误等警告,您可以在代码顶部编写以下内容:

import warnings
warnings.filterwarnings("error")
Run Code Online (Sandbox Code Playgroud)

但是如果代码库更大并且它正在导入一堆其他库/包,那么所有类型的警告都将开始作为错误出现。为了仅将某些类型的警告(在您的情况下,它的 FutureWarning)作为错误,您可以编写:

import warnings
warnings.simplefilter(action='error', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)


Lor*_*sum 12

@bdiamante的答案可能只会部分帮助您。如果您在取消警告后仍然收到消息,那是因为pandas库本身正在打印消息。除非您自己编辑Pandas源代码,否则您将无能为力。也许内部有一个抑制它们的选项,或者是一种覆盖事物的方法,但我找不到。


对于那些需要知道为什么的人...

假设您要确保干净的工作环境。在脚本的顶部,放pd.reset_option('all')。使用Pandas 0.23.4,您将获得以下内容:

>>> import pandas as pd
>>> pd.reset_option('all')
html.border has been deprecated, use display.html.border instead
(currently both are identical)

C:\projects\stackoverflow\venv\lib\site-packages\pandas\core\config.py:619: FutureWarning: html.bord
er has been deprecated, use display.html.border instead
(currently both are identical)

  warnings.warn(d.msg, FutureWarning)

: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

C:\projects\stackoverflow\venv\lib\site-packages\pandas\core\config.py:619: FutureWarning:
: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

  warnings.warn(d.msg, FutureWarning)

>>>
Run Code Online (Sandbox Code Playgroud)

按照@bdiamante的建议,您可以使用该warnings库。现在,诚如其言,警告已被删除。但是,仍然存在一些令人讨厌的消息:

>>> import warnings
>>> warnings.simplefilter(action='ignore', category=FutureWarning)
>>> import pandas as pd
>>> pd.reset_option('all')
html.border has been deprecated, use display.html.border instead
(currently both are identical)


: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

>>>
Run Code Online (Sandbox Code Playgroud)

实际上,禁用所有警告会产生相同的输出:

>>> import warnings
>>> warnings.simplefilter(action='ignore', category=Warning)
>>> import pandas as pd
>>> pd.reset_option('all')
html.border has been deprecated, use display.html.border instead
(currently both are identical)


: boolean
    use_inf_as_null had been deprecated and will be removed in a future
    version. Use `use_inf_as_na` instead.

>>>
Run Code Online (Sandbox Code Playgroud)

从标准库的角度来看,这些并不是真正的警告。熊猫实施自己的警告系统。运行grep -rn警告消息表明pandas警告系统已在core/config_init.py以下位置实现:

$ grep -rn "html.border has been deprecated"
core/config_init.py:207:html.border has been deprecated, use display.html.border instead
Run Code Online (Sandbox Code Playgroud)

进一步的追踪表明,我没有时间这样做。而且您可能也不是。希望这可以使您免于跌倒,或者可以激发某人找出如何真正抑制这些信息的方法!

  • 我发现这可以与“pandas”导入警告一起使用;warnings.filterwarnings("忽略") (3认同)
  • > 进一步的追击表明我没有时间这样做。你可能也不知道。我欠你一大笔钱,伙计。 (2认同)

小智 5

这是上下文管理器版本,如果您只想抑制特定代码行的警告。

import warnings
with warnings.catch_warnings():
    warnings.simplefilter(action='ignore', category=FutureWarning)
    # Warning-causing lines of code here
Run Code Online (Sandbox Code Playgroud)

  • 我觉得这应该是公认的答案。在模块级别禁用警告感觉是错误的。如果您知道要禁用的警告,您还应该知道来源并禁用该特定代码段。否则,您不知道您可能会禁用哪些其他警告。 (7认同)