title: How to shift a column in a pandas dataframe
body: <p>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</p>
  <pre><code>df['x'] = df.x.shift(1)
  </code></pre>
  <p>and</p>
  <pre><code>df['x'] = df['x'].shift(1)
  </code></pre>
  <p>some quick research told me that using .loc() should fix my issue, but I tried to do</p>
  <pre><code>df['x'] = df.loc[:, &quot;x&quot;].shift(1)
  </code></pre>
  <p>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</p>
  <p>Why is it still raising an error?
  Printing df.loc[:, &quot;x&quot;] tells me it is the correct series, and I used the recommended function to retrieve it.</p>
  <p>Edit: I tried doing this:</p>
  <pre><code>df.loc[:, 'x'] = df.loc[:, &quot;x&quot;].shift(shift)
  </code></pre>
  <p>and it still raised the same error</p>
  <p>Another Edit:</p>
  <pre><code>x= df.x.shift(1)
  </code></pre>
  <p>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.</p>