Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore SettingWithCopyWarning using warnings.simplefilter()?

The question:

Can I ignore or prevent the SettingWithCopyWarning to be printed to the console using warnings.simplefilter()?

The details:

I'm running a few data cleaning routines using pandas, and those are executed in the simplest of ways using a batch file. One of the lines in my Python script triggers the SettingWithCopyWarning and is printed to the console. But it's also being echoed in the command prompt:

enter image description here

Aside from sorting out the source of the error, is there any way I can prevent the error message from being printed to the prompt like I can with FutureWarnings like warnings.simplefilter(action = "ignore", category = FutureWarning)?

like image 577
vestland Avatar asked Jan 15 '19 11:01

vestland


People also ask

How do you ignore SettingWithCopyWarning?

One approach that can be used to suppress SettingWithCopyWarning is to perform the chained operations into just a single loc operation. This will ensure that the assignment happens on the original DataFrame instead of a copy. Therefore, if we attempt doing so the warning should no longer be raised.

How do I stop pandas warnings SettingWithCopyWarning?

Generally, to avoid a SettingWithCopyWarning in Pandas, you should do the following: Avoid chained assignments that combine two or more indexing operations like df["z"][mask] = 0 and df. loc[mask]["z"] = 0 . Apply single assignments with just one indexing operation like df.


2 Answers

Though I would strongly advise to fix the issue, it is possible to suppress the warning by importing it from pandas.core.common. I found where it's located on GitHub.

Example:

import warnings

import pandas as pd
from pandas.core.common import SettingWithCopyWarning

warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)

df = pd.DataFrame(dict(A=[1, 2, 3], B=[2, 3, 4]))
df[df['A'] > 2]['B'] = 5  # No warnings for the chained assignment!
like image 50
Georgy Avatar answered Sep 16 '22 16:09

Georgy


You can use:

pd.set_option('mode.chained_assignment', None)
# This code will not complain!
pd.reset_option("mode.chained_assignment")

Or if you prefer to use it inside a context:

with pd.option_context('mode.chained_assignment', None):
    # This code will not complain!
like image 35
Bruno Ruas De Pinho Avatar answered Sep 20 '22 16:09

Bruno Ruas De Pinho