0

I have a dataframe, and I need to shift all the elements in it by 1, and I've tried using .shift() but I keep getting a SettingWithCopyWarning exception. I first tried both

df['x'] = df.x.shift(1)

and

df['x'] = df['x'].shift(1)

some quick research told me that using .loc() should fix my issue, but I tried to do

df['x'] = df.loc[:, "x"].shift(1)

but it still raised the same error 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

Why is it still raising an error? Printing df.loc[:, "x"] tells me it is the correct series, and I used the recommended function to retrieve it.

Edit: I tried doing this:

df.loc[:, 'x'] = df.loc[:, "x"].shift(shift)

and it still raised the same error

Another Edit:

x= df.x.shift(1)

works fine, but when I try to assign x back to the df['x'] using either loc() or normal indexing, it still raises the same error. I read the pandas documentation on this warning and don't understand why this is still happening.

mbohde
  • 109
  • 1
  • 7
  • 1
    Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – semblable Apr 08 '21 at 02:14
  • @KristianCanler I tried using .loc as recommended in that thread and received the same error – mbohde Apr 08 '21 at 02:24
  • Sorry, that's an automatic comment when the question is reported as duplicate. But you should read the links in that answer (especially dataquest and realpython) because this is an important warning to understand. Adding `.copy()` can be another get around, but the real goal to to avoid repeated slicing assignment. – semblable Apr 08 '21 at 02:29
  • In this case I don't think we can really say what's happening specifically without a [example] – semblable Apr 08 '21 at 02:29
  • @Monte - Need `copy` for filtering before your code, not `loc`. If pass code to question before `df['x'] = df.x.shift(1)` I can explain more – jezrael Apr 08 '21 at 04:56

1 Answers1

0

You have another df when you filter to get your df adding copy

df = somedf[condition].copy()
BENY
  • 317,841
  • 20
  • 164
  • 234