Skip to content

WARN should SettingWithCopyWarning be shown multiple times in a Python REPL? #50209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MarcoGorelli opened this issue Dec 12, 2022 · 4 comments · Fixed by #56614
Closed

WARN should SettingWithCopyWarning be shown multiple times in a Python REPL? #50209

MarcoGorelli opened this issue Dec 12, 2022 · 4 comments · Fixed by #56614
Labels
Warnings Warnings that appear or should be added to pandas

Comments

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Dec 12, 2022

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 class DatetimeWarning that is a subclass of UserWarning, and then in errors\__init__.py, we add in warnings.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

@MarcoGorelli MarcoGorelli added the Warnings Warnings that appear or should be added to pandas label Dec 12, 2022
@Dr-Irv
Copy link
Contributor

Dr-Irv commented Dec 12, 2022

To get this to work for something like SettingWithCopyWarning, you just add this line to errors/__init__.py:

warnings.simplefilter("always", SettingWithCopyWarning)

Users can turn off the warnings by placing

warnings.simplefilter("ignore", SettingWithCopyWarning)

in their code.

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Dec 12, 2022

If we desire to always state a warning by default, we should evaluate it for any of the specialized pandas warnings:

    AccessorRegistrationWarning
    AttributeConflictWarning
    CategoricalConversionWarning
    CSSWarning
    DtypeWarning
    IncompatibilityWarning
    ParserWarning
    PerformanceWarning
    SettingWithCopyWarning

And from #49024 use the correct warning there

@rhshadrach
Copy link
Member

I'm generally positive, but I think we need to be careful with warnings that have been added with the expectation that they would only be seen once. E.g. it can happen that when looping over the columns of a DataFrame a warning gets emitted for each column. This type of behavior is what lead to the reverts of using find_stack_level in #44753 after a performance degradation showed up in ASVs. It was admittedly rare.

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Dec 12, 2022

I'm generally positive, but I think we need to be careful with warnings that have been added with the expectation that they would only be seen once.

That's why I suggested we look at each warning and decide whether we warn "always" or "once".

For the one in #49024, the warning gets emitted each time you call pd.to_datetime(), which is typically done on new data, so you'd want to find out which piece of data was emitting the warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants