Skip to content

BUG: DataFrame.rename() does not replace existing columns #46831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
xl0 opened this issue Apr 22, 2022 · 1 comment
Closed
2 of 3 tasks

BUG: DataFrame.rename() does not replace existing columns #46831

xl0 opened this issue Apr 22, 2022 · 1 comment

Comments

@xl0
Copy link

xl0 commented Apr 22, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame(index=[1, 2, 3], columns=["a"], data=[1, 2, 3])

df.rename(columns={"a" : "A"}, inplace=True)
df["a"] = [1, 2, 3]

df.rename(columns={"a" : "A"}, inplace=True)
print(df)

Issue Description

   A  A
1  1  1
2  2  2
3  3  3

DataFrame.rename() does not check if a column already exists.

Expected Behavior

DataFrame.rename() should replace the existing column:

   A
1  1
2  2
3  3

I would expect df.rename() to be equivalent to

df["A"] = df["a"],
df.drop(columns=["a"], inplace=True)

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.9.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-107-generic
Version : #121-Ubuntu SMP Thu Mar 24 16:04:27 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.2
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.31.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
brotli :
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.0
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@xl0 xl0 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 22, 2022
@phofl
Copy link
Member

phofl commented Apr 22, 2022

Hi, thanks for your report. This is the intended behavior. Please see #45486 for example

@phofl phofl closed this as completed Apr 22, 2022
@phofl phofl added Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 22, 2022
@phofl phofl added this to the No action milestone Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants