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

我想使用正则表达式来抑制特定类型的警告.
警告信息是:

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

我的抑制过滤器的方式:

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

然而,它失败了.我确实尝试将警告消息的不同部分粘贴到正则表达式中但仍然失败.我想知道为什么.

解决方法

这是你得到的错误吗?

AssertionError: message must be a string

warnings.filterwarnings只接受message参数的字符串,regex不会在那里编译.如果您确实想要抑制此错误,请执行以下操作:

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

否则,您可能想要查看避免SettingWithcopyWarning的方法,因为许多其他人已经解决了同样的问题:
How to deal with SettingWithCopyWarning in Pandas?

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...