@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
进一步的追逐表明我没有时间做这个。你可能也不知道。希望这可以使您免于掉入兔子洞,或者可能会激发某人弄清楚如何真正压制这些消息!