jlandercy
jlandercy

Reputation: 11002

Specificaly silent Pandas SettingWithCopyWarning using warnings context manager?

I have the policy of showing all warnings:

import warnings
warnings.simplefilter('always')

I would like to silent some false positive Pandas warnings using context managers:

with warnings.catch_warnings():
    warnings.filterwarnings('ignore', category=SettingWithCopyWarning)
    # Some assignment raising false positive warning that should be silenced

# Some assignment actually raising a true positive warning

But after having a look on Pandas source, I cannot found where the object SettingWithCopyWarning is defined in Pandas.

Does anyone know where this object is defined in Pandas namespace?

Upvotes: 3

Views: 2413

Answers (2)

jlandercy
jlandercy

Reputation: 11002

Merging information from comments into a single answer:

import warnings
import pandas as pd

As @Andrew pointed out, I can achieve it using dedicated Pandas Context Manager:

with pd.option_context('mode.chained_assignment', None):
    # Chaining Assignment, etc...

Or using the PSL warnings provided I can locate the warning SettingWithCopyWarning object (thanks to @coldspeed for the GitHub link):

with warnings.catch_warnings():
    warnings.filterwarnings('ignore', category=pd.core.common.SettingWithCopyWarning)
    # Chaining Assignment, etc...

Notice both solution seems to behave similarly but they are not exactly equivalent:

  • Pandas Context Manager temporarily alters Pandas options an then restores it;
  • PSL Context Manager catches a specific warning and silent it without altering Pandas options.

Additional information

It can be worthy to convert this specific warning into error:

pd.set_option('mode.chained_assignment', 'raise')

This will force your development to avoid those specific edge-cases and force your code to explicitly state if it works on view or only on a copy.

Off course the exception can be caught as usual:

try:
    # Chaining Assignment, etc...
except pd.core.common.SettingWithCopyError:
    pass

But in this case, converting warning into error will likely force you to modify the ambiguous code until the error vanishes instead of catching the related exception.

Observation

IMHO, completely silent those warnings using:

pd.set_option('mode.chained_assignment', None)

Is a bad practice, and does not help to produce better code.

Upvotes: 6

Andrew
Andrew

Reputation: 970

The following should do what you are looking for:

pd.set_option('mode.chained_assignment', None)

taken from https://www.dataquest.io/blog/settingwithcopywarning/

However, take some time to read the above article as it explains much about that warning. Perhaps you do not want to always silence it!

Upvotes: 3

Related Questions