How to ignore SettingWithCopyWarning using warnings.simplefilter()?

19,427

Solution 1

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!

Solution 2

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!
Share:
19,427
vestland
Author by

vestland

No mystery. Lots of air. And some resources: Small snippets of great use: # pandas dataframes in and out os.listdir(os.getcwd()) os.getcwd() os.chdir('C:/repos/py_research/import') df = pd.read_clipboard(sep='\\s+') df = df.astype(str) df = df.apply(lambda x: x.str.replace(',','.')) df = df.astype(float) df = pd.read_csv(r'C:\dbs.csv',sep = ",", header = None) df.to_csv(r'C:\dbs.csv', sep=',', float_format='%.2f', decimal = '.', index=False) # replaze zeros df = df.replace({'0':np.nan, 0:np.nan}) IPython magic %prun #Show how much time your program spent in each function !ls *.csv # execute shell command inside notebook A few SO posts I always come back to: SO link magic How to make good reproducible pandas examples Python Pandas Counting the Occurrences of a Specific value How to get all images posted by me? Some valuable resources: Plotly: Python figure reference Plotly: x-axis tickformat, dates Plotly: Scatter plots with python Plotly: Gantt charts with python IPython: 28 tips Google Chrome inspect elements Installations: conda config --set ssl_verify False # https://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html Anaconda installer archive Datasets: plotly Windows Commands https://www.howtogeek.com/194041/how-to-open-the-command-prompt-as-administrator-in-windows-8.1/ IDEs #VSCode https://code.visualstudio.com/docs/python/environments How to map arguments in functions: def SetColor(x): if(x == 'A'): return "1" elif(x == 'B'): return "2" elif(x == 'C'): return "3" lst = ['A', 'B', 'C'] list(map(SetColor, lst))

Updated on July 27, 2022

Comments

  • vestland
    vestland almost 2 years

    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)?