Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 206
jlandercy Avatar asked Dec 27 '25 23:12

jlandercy


1 Answers

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.

like image 107
jlandercy Avatar answered Dec 30 '25 16:12

jlandercy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!