如何抑制熊猫未来警告?


118

当我运行程序时,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) 

我得到了消息,但我只是想一次又一次地停止Pandas显示这样的消息,是否可以设置任何buildin参数,以使Pandas不会弹出“未来警告”?

Answers:


265

github上发现了这个...

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

import pandas

28
注意:把warnings....ignore 之前import pandas...造成FutureWarning被忽略。
迈克尔

18

@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)

>>>

按照@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.

>>>

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

>>> 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.

>>>

从标准库的角度来看,这些不是真正的警告。熊猫实施自己的警告系统。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

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


7

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

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

但是,如果要一一处理它们,并且要管理更大的代码库,将很难找到引起警告的代码行。由于警告与错误不同,因此代码回溯不会附带警告。为了跟踪类似错误的警告,您可以在代码顶部编写以下代码:

import warnings
warnings.filterwarnings("error")

但是,如果代码库更大,并且正在导入一堆其他库/程序包,则各种警告将开始作为错误发出。为了仅将某些类型的警告(在您的情况下为FutureWarning)引发为错误,您可以编写:

import warnings
warnings.simplefilter(action='error', category=FutureWarning)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.