10

The following line of my code causes a warning :

import pandas as pd

s = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
s.loc[-1] = [5,np.nan,np.nan,6]
grouped = s.groupby(['A'])
for key_m, group_m in grouped:
    group_m.loc[-1] = [10,np.nan,np.nan,10]

C:\Anaconda3\lib\site-packages\ipykernel\__main__.py:10: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

According to the documentation this is the recommended way of doing, so what is happening ?

Thanks for your help.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
Anthony Lethuillier
  • 1,489
  • 4
  • 15
  • 33
  • 1
    You need to show the complete code from the creation of the df to this line in order for us to assist – EdChum Jan 26 '17 at 09:18

1 Answers1

15

The documentation is slightly confusing.

Your dataframe is a copy of another dataframe. You can verify this by running bool(df.is_copy) You are getting the warning because you are trying to assign to this copy.

The warning/documentation is telling you how you should have constructed df in the first place. Not how you should assign to it now that it is a copy.

df = some_other_df[cols]

will make df a copy of some_other_df. The warning suggests doing this instead

df = some_other_df.loc[:, [cols]]

Now that it is done, if you choose to ignore this warning, you could

df = df.copy()

or

df.is_copy = None
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 1
    is_copy is deprecated. Is there a new technique to use that is "future-approved"? – Josiah Yoder Jun 29 '18 at 14:23
  • 1
    `df = df.copy()` should work just fine. I personally avoid editing dataframes in place. Each transformation produces a new copy and I overwrite the old one when desired. – piRSquared Jun 29 '18 at 14:37