Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 412
Peter Li Avatar asked Sep 06 '16 06:09

Peter Li


1 Answers

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.

like image 81
Daniel Ching Avatar answered Oct 02 '22 21:10

Daniel Ching