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 · 7 comments

Comments

@max-sixty
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Jun 17, 2016

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

This comment has been minimized.

Copy link
Contributor Author

commented Jun 18, 2016

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})

@gliptak gliptak referenced this issue Jun 30, 2016

Merged

Update documentation for rename #13533

2 of 4 tasks complete

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.18.2, 0.19.0 Jun 30, 2016

@jorisvandenbossche

This comment has been minimized.

Copy link
Member

commented Jun 30, 2016

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

This comment has been minimized.

Copy link
Contributor

commented Jul 1, 2016

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.

@apnewberry

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

commented Dec 7, 2018

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

@TomAugspurger

This comment has been minimized.

Copy link
Contributor

commented Dec 16, 2018

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.

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 4, 2019

ENH: Check that v is not a callable (pandas-dev#13473)
In this commit, the bug is removed from the code, which raised a
TypeError if a function was passed to rename instead of a dictionary.

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 4, 2019

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 4, 2019

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 4, 2019

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 5, 2019

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 5, 2019

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 5, 2019

MaxVanDeursen added a commit to MaxVanDeursen/pandas that referenced this issue Mar 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.