简体   繁体   English

为什么我不能使用 warnings.filterwarnings 用正则表达式抑制警告

[英]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"." r".*A Value is trying to.*"不匹配"\\nA value is trying to be.*"因为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")导入警告 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. warnings.filterwarnings只为message参数使用一个字符串,而regex不会在那里编译。 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? 否则,您可能想要检查避免SettingWithCopyWarning的方法,因为许多其他人也遇到过相同的问题: 如何在Pandas中处理SettingWithCopyWarning?

This is not how the filterwarnings works.这不是过滤器警告的工作方式。 In the documentation you can see Omitted arguments default to a value that matches everything.在文档中,您可以看到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 .您还可以看到: 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.如果您设置 message 参数,那么每个匹配的唯一文本消息将被显示一次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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