如何抑制Pandas未来的警告?

256
每次运行程序时,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停止反复显示这样的消息,有没有内置参数可以让Pandas不弹出“Future warning”?

6个回答

536

Github 上发现了这个...

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

import pandas

59
请在导入 pandas 之前加上 warnings....ignore 以忽略 FutureWarning。请注意不要改变原意。 - michael
29
嘿,尽管添加了这些行,我仍然收到未来警告。 - Eswar

74

如果您只想在特定的代码行中禁止警告,则可以使用上下文管理器版本。

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

6
我觉得这个回答应该被采纳。在模块级别禁用警告感觉不正确。如果你知道要禁用哪个警告,那么你也应该知道源代码并禁用特定的部分。否则,你就不知道可能会禁用哪些其他警告。 - Alfonso Irarrázaval
我认为你并不总是想要“禁用特定的代码片段”。我经常遇到以下情况(在原地设置值一直是我的意图)。DeprecationWarning: 在将来的版本中,df.iloc[:, i] = newvals 将尝试就地设置值,而不总是设置一个新的数组。为保留旧行为,请使用 df[df.columns[i]] = newvals 或者,如果列是非唯一的,请使用 df.isetitem(i, newvals) - Wesley Kitlasten

36

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

>>>

按标准库的定义,这些不是真正的警告。Pandas实现了自己的警告系统。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

进一步的追踪显示我没有时间处理这个问题。你可能也没有时间处理。希望这可以避免你陷入困境,或者激励某人找到如何真正抑制这些消息的方法!


5
我发现这个用于pandas的方法很有效。import warnings; warnings.filterwarnings("ignore") - metersk

21

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

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

但是,如果你想逐个处理它们并且管理的代码库很大,那么很难找到引起警告的代码行。由于警告不像错误一样附带代码跟踪,因此追踪警告需要在代码顶部编写以下内容:

import warnings
warnings.filterwarnings("error")

但如果代码库很大并且导入了许多其他的库/包,那么各种警告就会开始被视为错误。为了仅将特定类型的警告(在您的情况下,是FutureWarning)作为错误引发,您可以编写:

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

20

在开始编码之前,只需将此行放置即可。

import warnings
warnings.filterwarnings("ignore")

0

导入警告

warnings.filterwarnings('ignore')


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接