0

Often when using pandas I get UserWarning and PerformanceWarning messages like these:

C:\Users\User\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py:558: UserWarning: merging between different levels can give an unintended result (2 levels on the left, 1 on the right) warnings.warn(msg, UserWarning) C:\Users\User\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py:558: UserWarning: merging between different levels can give an unintended result (1 levels on the left, 2 on the right) warnings.warn(msg, UserWarning) C:\Users\User\Anaconda3\lib\site-packages\pandas\core\generic.py:2530: PerformanceWarning: dropping on a non-lexsorted multi-index without a level parameter may impact performance. obj = obj._drop_axis(labels, axis, level=level, errors=errors) 

When writing large scripts, I find difficult to know what in my code is the source of the warning.

So how can I figure out what line of my source code generates the warning messages?

2 Answers 2

3

One approach that I often use is to configure the filterwarnings() method in the warnings package to filter the warnings to raise which will enable you to debug them (e.g., using pdb). To do this you just need to import the warnings package and then set the filterwarnings() method on warnings to watch and raise specific warnings, like this:

import warnings warnings.filterwarnings('error', category=UserWarning) warnings.filterwarnings('error', category=PerformanceWarning) 

You can also just configure warnings to raise any warning, like this:

import warnings warnings.filterwarnings('error') 

You can learn more about using pdb here: https://docs.python.org/3/library/pdb.html

Sign up to request clarification or add additional context in comments.

4 Comments

Good catch! Sorry I didn't catch it myself.
Initially I used the second option, as PerformanceWarning is not defined in the Warning classes of the package. Since the PerformanceWarning bothers me most, I've looked for a way to filter just that, and found this tip from one of pandas contribuitors: warnings.filterwarnings('error',category=pandas.io.pytables.PerformanceWarning) where I found: github.com/pandas-dev/pandas/issues/3622
Couldn't edit the comment so just removed and added another, sorry for the mass and thank you very much for the help!
Anytime my friend! :)
2

If your intention is to resolve the underlying issues, you can use the -W option on the command-line to turn the warning into an error, which will give you the full stack trace.

From the command-line documentation:

-W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg

E.g., to trap the warnings that start with "merging between different levels...", you could run:

python -Werror:merging myscript.py 

1 Comment

This is a good tip and a better answer. It helped me identify the specific line of code which triggered the PerformanceWarning mentioned in the question. I changed the word merging to dropping to find it. python -Werror:dropping myscript.py

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.