Skip to content

API/ENH: Detect trying to set inplace on copies in a nicer way, related (GH5597) #5679

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

Merged
merged 2 commits into from
Dec 11, 2013

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Dec 11, 2013

related #5597
pretty common error that I have seen, originally from:

http://stackoverflow.com/questions/20508968/series-fillna-in-a-multiindex-dataframe-does-not-fill-is-this-a-bug

In [1]: a = [12, 23]

In [2]: b = [123, None]

In [3]: c = [1234, 2345]

In [4]: d = [12345, 23456]

In [5]: tuples = [('eyes', 'left'), ('eyes', 'right'), ('ears', 'left'), ('ears', 'right')]

In [6]: events = {('eyes', 'left'): a, ('eyes', 'right'): b, ('ears', 'left'): c, ('ears', 'right'): d}

In [7]: multiind = pandas.MultiIndex.from_tuples(tuples, names=['part', 'side'])

In [8]: zed = pandas.DataFrame(events, index=['a', 'b'], columns=multiind)

Shows a warning (the message is comewhat generic)

In [9]: zed['eyes']['right'].fillna(value=555, inplace=True)
/usr/local/bin/ipython:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
  #!/usr/local/bin/python

In [10]: zed
Out[10]: 
part  eyes         ears       
side  left  right  left  right
a       12    123  1234  12345
b       23    NaN  2345  23456

[2 rows x 4 columns]

Correct method

In [11]: zed.loc[:,('eyes','right')].fillna(value=555, inplace=True)

In [12]: zed
Out[12]: 
part  eyes         ears       
side  left  right  left  right
a       12    123  1234  12345
b       23    555  2345  23456

[2 rows x 4 columns]

@jreback
Copy link
Contributor Author

jreback commented Dec 11, 2013

cc @michaelaye .... I know you use lots of multi-index stuff...can you give this a quick test run and lmk if any SettingWithCopyError/Warns come up (that are unexpected). ? thxs

@michaelaye
Copy link
Contributor

I tested 222bc9d and got no Error/Warns.
But I got to warn you that I don't have much multi-index stuff anymore. The issues we discussed recently were months old, and because of the pending issues that I partly didn't understand, and because they are harder to grasp in general I refrained from using them and solved my stuff mostly with 1-D indexes.

@jreback
Copy link
Contributor Author

jreback commented Dec 11, 2013

@michaelaye gr8 thanks. It was also trying to do inplace operations when operating on a copy. So you code seems good.

jreback added a commit that referenced this pull request Dec 11, 2013
API/ENH: Detect trying to set inplace on copies in a nicer way, related (GH5597)
@jreback jreback merged commit ceec8bf into pandas-dev:master Dec 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants