简体   繁体   中英

why I cannot suppress warning with regex using warnings.filterwarnings

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.

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.

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

Was this the error you got?

AssertionError: message must be a string

warnings.filterwarnings only takes a string for the message argument and regex wouldn't compile there. If you really want to suppress this error, do:

import pandas as pd
warnings.simplefilter("error", pd.core.common.SettingWithCopyWarning)

Otherwise, you might want to check out ways to avoid SettingWithCopyWarning as many other people have encounted the same issue: How to deal with SettingWithCopyWarning in Pandas?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM