Skip to content

DataFrame.rename() function not throwing Error when non existing column pass in dict #23043

Closed
@neelpuniwala

Description

@neelpuniwala

Code

df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
df.rename(index=str, columns={"C": "a", "B": "c"},inplace = True)
df

#Output
        A       c
0	1	4
1	2	5
2	3	6

Problem description

the current behaviour of code is accepting any rename column dictionary. If rename column found in DataFrame, code renamed it but if rename column not found,Ideally, it should throw KeyError. Function will become more robust .

Expected Output

KeyError: 'C'

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-124-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_IN LOCALE: en_IN.ISO8859-1

pandas: 0.23.4
pytest: None
pip: 18.0
setuptools: 40.0.0
Cython: None
numpy: 1.15.0
scipy: None
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Activity

TomAugspurger

TomAugspurger commented on Oct 8, 2018

@TomAugspurger
Contributor

Duplicate of #13473

The current behavior is sometimes useful. We'd be happy to have an errors keyword to control how missing keys should be handled.

added this to the No action milestone on Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @TomAugspurger@neelpuniwala

        Issue actions

          DataFrame.rename() function not throwing Error when non existing column pass in dict · Issue #23043 · pandas-dev/pandas