Skip to content
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

False negative on SettingWithCopyWarning #9767

Closed
adamklein opened this issue Mar 31, 2015 · 5 comments · Fixed by #56614
Closed

False negative on SettingWithCopyWarning #9767

adamklein opened this issue Mar 31, 2015 · 5 comments · Fixed by #56614
Labels
Bug Copy / view semantics Indexing Related to indexing on series/frames, not to indexes themselves
Projects

Comments

@adamklein
Copy link
Contributor

This is pandas 0.15.2

In [1]: from pandas import *

In [2]: df = DataFrame({"A" : [1,2,3,4,5], "B": [3.1, 4, 5, 6, 7]})

In [3]: df[["A", "B"]]["A"] = 5  # no warning - bug?!

In [4]: df
Out[4]:
A    B
0  1  3.1
1  2  4.0
2  3  5.0
3  4  6.0
4  5  7.0

In [5]: df[["A"]]["A"] = 5
C:\Anaconda\Scripts\ipython-script.py: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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':
@jorisvandenbossche
Copy link
Member

Confirmed with master

@jorisvandenbossche jorisvandenbossche added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Apr 1, 2015
@jreback
Copy link
Contributor

jreback commented Apr 2, 2015

hmm, at least False Positives are over.

@jreback jreback added this to the Next Major Release milestone Apr 2, 2015
@OmerJog
Copy link

OmerJog commented Nov 13, 2019

This is still an issue in master

@jbrockmendel jbrockmendel added this to SettingWithCopyWarning in Indexing Feb 18, 2020
@erfannariman
Copy link
Member

What is the expected behaviour here?

@bersbersbers
Copy link

Here's another one:

import pandas as pd

index = pd.MultiIndex.from_product([["vars"], ["fruit", "color"]])
df = pd.DataFrame([["apple", "green"]], columns=index)

# No warning!
df.vars.color = "red"

# Still green!
print(df)

# No effect, but warning: A value is trying to be set on a copy of a slice ...
df.vars.color[0] = "red"

After df.vars.color = "red", color is not set to "red", and there's no warning.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Copy / view semantics Indexing Related to indexing on series/frames, not to indexes themselves
Projects
No open projects
Indexing
Views vs Copies
Development

Successfully merging a pull request may close this issue.

8 participants