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.

Peter Li
  • 341
  • 3
  • 8
  • Are you sure your warning filter is set before the warning is actually raised? – Tryph Sep 06 '16 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, – Peter Li Sep 07 '16 at 01:09

3 Answers3

6

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.

Daniel Ching
  • 443
  • 5
  • 9
0

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
  • 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. – Silas Jan 14 '22 at 11:37
-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")

sara
  • 45
  • 2
  • 13