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

SettingWithCopyWarning for string manipulation in dataframes with a non-monotonic index #6025

Closed
egrublyte opened this issue Jan 21, 2014 · 2 comments · Fixed by #6042
Closed
Milestone

Comments

@egrublyte
Copy link

Hi,

I get a SettingWithCopyWarning for string manipulation in dataframes with a non-monotonic index.

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead

This only happens if the integer index skips a value. The code to reproduce:

from pandas import DataFrame
# create example dataframe:
df = DataFrame ({'column1':['a', 'a', 'a'], 'column2': [4,8,9] })
df
# assign string to 'column1':
df['column1'] = df['column1'] + 'b'
df
# it works just fine - no warnings
#now remove one line from dataframe df:
df = df [df['column2']!=8]
df
# adding string to 'column1' gives warning:
df['column1'] = df['column1'] + 'c'
df

NB: this only happens after a fresh login into python; if you run it again, it will not appear any more (!)

System:

  • Pandas 0.13
  • Python 2.7.2
  • Ubuntu 12.04LTS 64bit

Is this is an actual issue or just a nonsensical warning?

Kind regards,
Egle

@jreback
Copy link
Contributor

jreback commented Jan 21, 2014

this is a correct warning (though the message in this case is a bit inapplicable).

You are in fact setting a copy.

You prob don't care; it is mainly to address situations like:

df['foo'][0] = 123... which sets the copy (and thus is not visible to the user)

This operation, make the df now point to a copy of the original

df = df [df['column2']!=8]

If you don't care about the 'original' frame, then its ok

If you are expecting that the

df['column1'] = df['columns'] + 'c'

would actually set the original frame (they are both called 'df' here which is confusing)
then you would be suprised.

You can completly turn off this warning by setting pd.set_option('chained_assignement',None)
(this warning is mainly for new users to avoid setting the copy)

or you could do:

df.is_copy = False (after the indexing operation)

Note that when you have the same index as the original frame, this DOES NOT create a copy, so no warning is generated, it is only because of the indexing which ultimately calls take which causes a copy.

Copies don't normally matter except when you are then trying to set them in a chained manner.

@egrublyte
Copy link
Author

Thank you for the detailed explanation!

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 a pull request may close this issue.

2 participants