Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df = pd.DataFrame(columns=['foo','bar'],data=[[1,2],[1,2]])
for dataframe in [df]:
dataframe = dataframe.rename(columns={
'foo':'A',
'bar':'B'
})
print(df.columns)
Issue Description
Dataframe.rename() does not change column names as expected, when used in a for loop.
Expected Behavior
Dataframe to update with new column names, o.e. 'foo' -> 'A' and 'bar' -> 'B'
Installed Versions
Comment From: jbrockmendel
when you call .rename you're getting a new DataFrame object back, so shouldn't expect df
to be altered.
Comment From: phofl
Agreed, this behaves as expected
Comment From: ua-chjb
Sorry, but I don’t understand. How is calling that any different than just assigning it to itself outside of a for loop?
Comment From: jbrockmendel
The loop is irrelevant. Consider:
import pandas as pd
df = pd.DataFrame(columns=['foo','bar'],data=[[1,2],[1,2]])
dataframe = df
dataframe = dataframe.rename(columns={
'foo':'A',
'bar':'B'
})
print(df.columns)
When you do this, you no longer have dataframe is df
.
Comment From: ua-chjb
Clear. Thank you!