10

I want to suppress a specific type of warning using regular expression. The warning message is:

C:\Anaconda3\lib\site-packages\pandas\core\indexing.py:420: SettingWithCopyWarning:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
self.obj[item] = s

My way of suppressing filter:

import warnings
warnings.filterwarnings("ignore", message= ".*A value is trying to.*")

However, it failed. I did try pasting different part of the warning message into the regex but still failed. I want to know why.

2
  • Are you sure your warning filter is set before the warning is actually raised? Commented Sep 6, 2016 at 17:10
  • I am using the Jupyter Notebook. I am sure I executed the cell of the filter first then another cell with the actual logic. Still a warning, Commented Sep 7, 2016 at 1:09

3 Answers 3

8

Your regular expression does not match the correct message string.

r".*A Value is trying to.*" does not match "\nA value is trying to be.*" because r"." matches everything except the newline character.

Sometimes it can be tricky to figure out what the actual message string is without looking at the source code of the module that generated the warning.

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

1 Comment

Shouldn't .* match the empty string too, though? I suspect that's not the reason.
2

This is not how the filterwarnings works. In the documentation you can see Omitted arguments default to a value that matches everything. and also you can see:message (default '') : is a string containing a regular expression that start of the warning message must match.

This can be understood as using action "once" will affect each unique text message to display once. If you have a field in message that may change (for example filename), the warning will be displayed once for each filename.

If you set the message argument, than each matching unique text message will be diplayed once.

Here is a small example:

import warnings
import random

warnings.filterwarnings("ignore", category=UserWarning)
warnings.warn("You won't see this warning")

message_formater = "this message with number {} will be displayed once"

# deactivating previously created filter
warnings.resetwarnings()
# applying new filter
warnings.filterwarnings("once", message_formater.format("(.*)"), category=UserWarning)

for i in range(100):
    warnings.warn(message_formater.format(random.randint(0, 3)))
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 0 will be displayed once

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 3 will be displayed once

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 1 will be displayed once

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 2 will be displayed once

1 Comment

Although not the answer to OP, this is exactly the problem I had. The documentation is not very clear that all unique messages will be printed at least once, unless they are completely ignored. Warnings often have dynamic text, so would be nice to be able to suppress all but the first message that match the regex.
-1

Try giving only the below code(without message).. May be the message you've mentioned is not matching with the warnings.

import warnings warnings.filterwarnings("ignore")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.