Description
This came out of a discussion here: #49024 (comment)
When running pandas in a Python REPL, this warning is only shown once per session.
So, if it was raised from two different objects, a user could miss it in the second one:
>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
>>> df2 = df[df['a']>1]
>>> df2['b'] = 3 # warns
<stdin>:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
>>> df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
>>> df2 = df[df['a']>1]
>>> df2['b'] = 3 # no warning!
Should it be shown multiple times?
There's a suggestion for how to do this here:
I think we could get around this. If we create a warning in
errors\__init__.py
of classDatetimeWarning
that is a subclass ofUserWarning
, and then inerrors\__init__.py
, we add inwarnings.filter("always", DatetimeWarning)
then the warning will get emitted every time it happens.Concern here is that you are using the python interpreter, and you get the warning on one line, but not on a second line.
Also, by creating a specific class for this particular warning, it let's people test for it with their own filter.
I've opened this issue to split out the conversation, as it's not specific to #49024