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

NSTALLED VERSIONS ------------------ commit : 4bfe3d07b4858144c219b9346329027024102ab6 python : 3.8.9.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19041 machine : AMD64 processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252 pandas : 1.4.2 numpy : 1.22.3 pytz : 2021.1 dateutil : 2.8.1 pip : 20.2.3 setuptools : 49.2.1 Cython : 0.29.28 pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 4.8.0 html5lib : 1.1 pymysql : None psycopg2 : 2.8.6 jinja2 : 3.0.1 IPython : 7.22.0 pandas_datareader: None bs4 : 4.9.3 bottleneck : None brotli : 1.0.9 fastparquet : None fsspec : 2022.3.0 gcsfs : None markupsafe : 2.0.1 matplotlib : 3.4.1 numba : None numexpr : 2.8.1 odfpy : None openpyxl : 3.0.7 pandas_gbq : None pyarrow : 4.0.0 pyreadstat : 1.2.0 pyxlsb : None s3fs : 2022.3.0 scipy : 1.6.3 snappy : None sqlalchemy : 1.4.20 tables : None tabulate : 0.8.9 xarray : 0.18.2 xlrd : 2.0.1 xlwt : None zstandard : 0.17.0

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!