Skip to content
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

ENH: add errors='raise' option to rename #13473

Closed
max-sixty opened this issue Jun 17, 2016 · 10 comments · Fixed by #13533 or #25535
Closed

ENH: add errors='raise' option to rename #13473

max-sixty opened this issue Jun 17, 2016 · 10 comments · Fixed by #13533 or #25535

Comments

@max-sixty
Copy link
Contributor

max-sixty commented Jun 17, 2016

Is there a reason that .rename doesn't raise if keys are supplied that don't exist? This caught me out a couple of times:

In [14]: import pandas as pd

In [15]: df=pd.DataFrame({'a': [1,2], 'b': [3,4]})

In [16]: df
Out[16]: 
   a  b
0  1  3
1  2  4

In [17]: df.rename(columns={'a': 'c'})
Out[17]: 
   c  b
0  1  3
1  2  4
# ok

In [18]: df.rename(columns={'d': 'c'})
Out[18]: 
   a  b
0  1  3
1  2  4

# this should raise?
@max-sixty max-sixty changed the title BUG: Rename ignores BUG: Rename ignores keys that don't exist Jun 17, 2016
@TomAugspurger
Copy link
Contributor

I suspect it is intentional. Either way, the behavior should be clarified in the docs. Missing keys (in columns but not in the dict) are explicitly documented.

I know I've used this in the past. For example a bunch of CSVs with variations on the same items. I'll make one dictionary with all the mappings

renamer = {'zip': 'zip_code', 'postal code': 'zip_code', ...}`

And use that same renamer on many dataframes.

@max-sixty
Copy link
Contributor Author

OK. You know this better than me - do you think this is too much vs the increase in explicitness?

df.rename(columns={col: renamer[col] for col in renamer if col in df.columns})

@jorisvandenbossche
Copy link
Member

For DataFrame.drop, the errors keyword was added some time ago:

errors : {'ignore', 'raise'}, default 'raise'
    If 'ignore', suppress error and existing labels are dropped.

    .. versionadded:: 0.16.1

We could maybe add this to rename as well to have similar functionality? (although the default value would be different for rename)

@TomAugspurger
Copy link
Contributor

Having a keyword to control this would be great.

Another option is to revisit the "tell people to use relabel discussion". If we implemented it there we could have the default be to raise.

On Jun 30, 2016, at 18:02, Joris Van den Bossche notifications@github.com wrote:

For DataFrame.drop, the errors keyword was added some time ago:

errors : {'ignore', 'raise'}, default 'raise'
If 'ignore', suppress error and existing labels are dropped.

.. versionadded:: 0.16.1

We could maybe add this to rename as well to have similar functionality? (although the default value would be different for rename)


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@ghost
Copy link

ghost commented Dec 7, 2018

Is there a particular reason why the default value would be "ignore" for rename? If I'm renaming keys in a mapping but one isn't there, I normally anticipate a KeyError. A deprecation period would be necessary, of course -- I think a similar change has occurred recently with missing columns for loc.

@WillAyd
Copy link
Member

WillAyd commented Dec 7, 2018

Historical reasons. I think raising would be preferable though correct would require deprecation cycle

@TomAugspurger
Copy link
Contributor

To be clear, I don't think there's a consensus for deprecating the current behavior. Just adding the option to raise when not present.

@bala-analytics
Copy link

This issue is closed but the problem still persists.

@TomAugspurger
Copy link
Contributor

@bala-analytics the original example correctly raises with df.rename(columns={'d': 'c'}, errors="raise").

@bala-analytics
Copy link

bala-analytics commented Jun 17, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants