2

I have got this 'change on copy' warning during a column rename with df1.rename():

import pandas as pd    

df = pd.DataFrame({'a': (1,2,3),
                   'b': (4,5,6),
                   'c': (7,8,9)})

df1 = df[['a', 'b']]

#df1.columns = ('x', 'y') # Displays nothing
df1.rename(columns={df1.columns[0]: 'x', df1.columns[1]: 'y'}, inplace=True) # Displays the warning

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

However I don't get this warning when I completely change the column headers using df1.columns = ()

I don't get the rationale for the difference, I assumed both operations are equivalent, for example no difference is mentioned in answers to this question.


Note I'm not asking how to work on a copy of the original data, which indeed would suppress the warning. I'm asking about the difference between the two methods to rename series.

mins
  • 6,478
  • 12
  • 56
  • 75
  • You are seeing the `SettingWithCopyWarning` because you pass the `inplace=True` to the `rename` method. The rationale for the `SettingWithCopyWarning` itself is given [here](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#evaluation-order-matters) in the docs. – Mortz Dec 31 '21 at 15:39

2 Answers2

1

Use .loc to make a copy:

df1 = df.loc[:, ['a', 'b']]
df1.rename(columns={df1.columns[0]: 'x', df1.columns[1]: 'y'}, inplace=True)

To know more: Returning a view versus a copy

Corralien
  • 109,409
  • 8
  • 28
  • 52
1

Use .copy() or .loc to make a copy:

df1=df[['a', 'b']].copy()
# or
df1=df.loc[:,[df.columns[0],df.columns[1]]
Yağız
  • 61
  • 4