big*_*bug 92 suppress-warnings pandas
当我运行程序时,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)
Run Code Online (Sandbox Code Playgroud)
我得到了消息,但我只想阻止Pandas一次又一次地显示这样的消息,是否有任何buildin参数我可以设置让Pandas不会弹出'Future warning'?
bdi*_*nte 209
在github上发现这个...
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)
小智 21
只需在开始代码之前放置此行即可。
import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)
Alo*_*yak 14
警告很烦人。正如其他答案中所述,您可以使用以下方法抑制它们:
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)
但是如果你想一一处理它们并且你正在管理一个更大的代码库,将很难找到导致警告的代码行。由于与错误不同的警告不带有代码回溯。为了跟踪错误等警告,您可以在代码顶部编写以下内容:
import warnings
warnings.filterwarnings("error")
Run Code Online (Sandbox Code Playgroud)
但是如果代码库更大并且它正在导入一堆其他库/包,那么所有类型的警告都将开始作为错误出现。为了仅将某些类型的警告(在您的情况下,它的 FutureWarning)作为错误,您可以编写:
import warnings
warnings.simplefilter(action='error', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)
Lor*_*sum 12
@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)
>>>
Run Code Online (Sandbox Code Playgroud)
按照@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.
>>>
Run Code Online (Sandbox Code Playgroud)
实际上,禁用所有警告会产生相同的输出:
>>> 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.
>>>
Run Code Online (Sandbox Code Playgroud)
从标准库的角度来看,这些并不是真正的警告。熊猫实施自己的警告系统。运行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
Run Code Online (Sandbox Code Playgroud)
进一步的追踪表明,我没有时间这样做。而且您可能也不是。希望这可以使您免于跌倒,或者可以激发某人找出如何真正抑制这些信息的方法!
小智 5
这是上下文管理器版本,如果您只想抑制特定代码行的警告。
import warnings
with warnings.catch_warnings():
warnings.simplefilter(action='ignore', category=FutureWarning)
# Warning-causing lines of code here
Run Code Online (Sandbox Code Playgroud)