简体   繁体   English

如何抑制 Pandas Future 警告?

[英]How to suppress Pandas Future warning ?

When I run the program, Pandas gives 'Future warning' like below every time.当我运行程序时,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) 

I got the msg, but I just want to stop Pandas showing such msg again and again, is there any buildin parameter that I can set to let Pandas not pop up the 'Future warning' ?我收到了 msg,但我只想阻止 Pandas 一次又一次地显示这样的 msg,是否有任何内置参数可以设置让 Pandas 不弹出“未来警告”?

Found this on github ...github上找到了这个...

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

import pandas

@bdiamante's answer may only partially help you. @bdiamante 的回答可能只能部分帮助您。 If you still get a message after you've suppressed warnings, it's because the pandas library itself is printing the message.如果您在抑制警告后仍然收到一条消息,那是因为pandas库本身正在打印该消息。 There's not much you can do about it unless you edit the Pandas source code yourself.除非您自己编辑 Pandas 源代码,否则您无能为力。 Maybe there's an option internally to suppress them, or a way to override things, but I couldn't find one.也许内部有一个选项可以抑制它们,或者是一种覆盖事物的方法,但我找不到。


For those who need to know why...对于那些需要知道为什么...

Suppose that you want to ensure a clean working environment.假设您想确保一个干净的工作环境。 At the top of your script, you put pd.reset_option('all') .在脚本的顶部,放置pd.reset_option('all') With Pandas 0.23.4, you get the following:使用 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)

>>>

Following the @bdiamante's advice, you use the warnings library.按照@bdiamante 的建议,您可以使用warnings库。 Now, true to it's word, the warnings have been removed.现在,言归正传,警告已被删除。 However, several pesky messages remain:但是,仍然存在一些令人讨厌的消息:

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

>>>

In fact, disabling all warnings produces the same output:事实上,禁用所有警告会产生相同的输出:

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

>>>

In the standard library sense, these aren't true warnings .在标准库的意义上,这些都不是真正的警告 Pandas implements its own warnings system. Pandas 实现了自己的警告系统。 Running grep -rn on the warning messages shows that the pandas warning system is implemented in core/config_init.py :运行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

Further chasing shows that I don't have time for this.进一步追赶表明我没有时间这样做。 And you probably don't either.你可能也没有。 Hopefully this saves you from falling down the rabbit hole or perhaps inspires someone to figure out how to truly suppress these messages!希望这能让您免于跌入兔子洞,或者激发某人找出如何真正抑制这些消息!

Warnings are annoying.警告很烦人。 As mentioned in other answers, you can suppress them using:正如其他答案中所述,您可以使用以下方法抑制它们:

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

But if you want to handle them one by one and you are managing a bigger codebase, it will be difficult to find the line of code which is causing the warning.但是如果你想一一处理它们并且你正在管理一个更大的代码库,将很难找到导致警告的代码行。 Since warnings unlike errors don't come with code traceback.由于与错误不同的警告不带有代码回溯。 In order to trace warnings like errors, you can write this at the top of the code:为了跟踪错误等警告,您可以在代码顶部编写以下内容:

import warnings
warnings.filterwarnings("error")

But if the codebase is bigger and it is importing bunch of other libraries/packages, then all sort of warnings will start to be raised as errors.但是如果代码库更大并且它正在导入一堆其他库/包,那么所有类型的警告都将开始作为错误出现。 In order to raise only certain type of warnings (in your case, its FutureWarning) as error, you can write:为了仅将某些类型的警告(在您的情况下,它的 FutureWarning)作为错误,您可以编写:

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

Here is the context manager version, if you only want to suppress warnings for specific lines of code.这是上下文管理器版本,如果您只想抑制特定代码行的警告。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM