warnings.filterwarning 的问题(你知道的可重现性最小)

issue with warnings.filterwarning (with minimal reproducible you know)

我无法理解以下代码有什么问题:

import warnings

warnings.filterwarnings('ignore', message='frequency information was provided')
warnings.warn("No frequency information was provided, so inferred frequency MS will be used.")

为什么警告通过过滤器?

警告过滤器的 message 是不区分大小写的正则表达式,警告消息的 start 必须匹配才能应用过滤器。

'frequency information was provided'"No frequency information was provided, so inferred frequency MS will be used."

start 不匹配

来自filterwarnings docs:

This checks the types of the arguments, compiles the message and module regular expressions, and inserts them as a tuple in the list of warnings filters.

message 参数是一个 正则表达式 。它与您消息的 biginning 匹配。但是,如果将 filterwarnings 语句更改为:

warnings.filterwarnings('ignore', message='.*frequency information was provided')

消息将与您的警告相匹配,警告将被忽略。