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.