如何抑制熊猫未来警告?

时间:2013-04-03 02:37:49

标签: pandas suppress-warnings

当我运行程序时,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不会弹出'Future warning'?

5 个答案:

答案 0 :(得分:148)

github ...

上找到了这个
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

答案 1 :(得分:4)

@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

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

答案 2 :(得分:4)

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

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

答案 3 :(得分:1)

警告很烦人。如其他答案中所述,您可以使用以下方法禁止显示它们:

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

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

import warnings
warnings.filterwarnings("error")

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

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

答案 4 :(得分:-1)

我尝试过,对我有用:

import warnings
warnings.filterwarnings("ignore", category=FutureWarning)