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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With