Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out where a Python Warning is from

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

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):

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.

like image 733
Bonswouar Avatar asked Jun 20 '13 08:06

Bonswouar


People also ask

How do you filter out a warning in Python?

Use the filterwarnings() Function to Suppress Warnings in Python. The warnings module handles warnings in Python. We can show warnings raised by the user with the warn() function. We can use the filterwarnings() function to perform actions on specific warnings.

How do you stop a runtime warning in Python?

Print the warning the first time it is generated from each module. Print the warning the first time it is generated. Following interactive session sets filter to default by simplefilter() function. In order to temporarily suppress warnings, set simplefilter to 'ignore'.

Which method is used to display a warning message in Python?

showwarning() This method is used to display the warning to the user.

Why do we import warnings in Python?

Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn't warrant raising an exception and terminating the program. For example, one might want to issue a warning when a program uses an obsolete module.


1 Answers

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

import warnings warnings.filterwarnings('error') 

*The warnings filter can 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.")

like image 172
Andy Hayden Avatar answered Sep 28 '22 14:09

Andy Hayden