pandas 如何找出 Python 警告的来源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17208567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:56:16  来源:igfitidea点击:

How to find out where a Python Warning is from

pythondebuggingwarningspandas

提问by Bonswouar

I'm still kinda new with Python, using Pandas, and I've got some issues debugging my Python script.

我对 Python 还是有点陌生​​,使用 Pandas,并且在调试 Python 脚本时遇到了一些问题。

I've got the following warning message :

我收到以下警告消息:

[...]\pandas\core\index.py:756: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
return self._engine.get_loc(key)

And can't find where it's from.

而且找不到出处。

After some research, I tried to do that in the Pandas lib file (index.py):

经过一番研究,我尝试在 Pandas lib 文件 (index.py) 中执行此操作:

try:
    return self._engine.get_loc(key)
except UnicodeWarning:
    warnings.warn('Oh Non', stacklevel=2)

But that didn't change anything about the warning message.

但这并没有改变警告消息的任何内容。

采纳答案by Andy Hayden

You can filter the warningsto raise which will enable you to debug (e.g. using pdb):

您可以过滤要引发的警告,这将使您能够进行调试(例如使用 pdb):

import warnings
warnings.filterwarnings('error')

*The warnings filtercan be managed more finely (which is probably more appropriate) e.g.:

*可以更精细地管理警告过滤器(这可能更合适),例如:

warnings.filterwarnings('error', category=UnicodeWarning)
warnings.filterwarnings('error', message='*equal comparison failed*')

Multiple filters will be looked up sequentially. ("Entries closer to the front of the list override entries later in the list, if both match a particular warning.")

将依次查找多个过滤器。(“靠近列表前面的条目会覆盖列表后面的条目,如果两者都匹配特定警告。”)

回答by Maxime R.

You can also use the commandline to control the warnings:

您还可以使用命令行来控制警告:

python -W error::UnicodeWarning your_code.py

From the man page:

从手册页:

-W argument
[...] errorto raise an exception instead of printing a warning message.

-W 参数
[...]错误以引发异常而不是打印警告消息。

This will have the same effect as putting the following in your code:

这与将以下内容放入您的代码具有相同的效果:

import warnings
warnings.filterwarnings('error', category=UnicodeWarning)

As was already said in Andy's answer.

正如安迪的回答中已经说过的那样。

回答by robjohncox

If you enable logging in python, then when an exception is received you can use the method logging.exceptionto log when an exception has been caught - this method will print out a nicely formatted stack trace that shows you exactly in the code where the exception originated. See the python document on loggingfor more information.

如果您在 python 中启用日志记录,那么当接收到异常时,您可以使用该方法logging.exception在捕获到异常时进行记录 - 此方法将打印出格式良好的堆栈跟踪,在异常发生的代码中准确显示您。有关更多信息,请参阅有关日志记录python 文档

import logging
log = logging.getLogger('my.module.logger')

try:
    return self._engine.get_loc(key)
except UnicodeWarning:
    log.exception('A log message of your choosing')

Alternatively, you can get a tuple that contains details of the exception in your code by calling sys.exc_info()(this requires you to import the sysmodule).

或者,您可以通过调用获取包含代码中异常详细信息的元组sys.exc_info()(这需要您导入sys模块)。