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

Add method to change the .name attribute on the axes #11965

Closed
wcbeard opened this issue Jan 5, 2016 · 5 comments
Closed

Add method to change the .name attribute on the axes #11965

wcbeard opened this issue Jan 5, 2016 · 5 comments
Labels
API Design Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@wcbeard
Copy link
Contributor

wcbeard commented Jan 5, 2016

It would be nice to have a method to change the .name attribute on the columns/index, to permit it in a method chain. (This is a suggestion based on this stackoverflow question.)

Instead of

df.columns.name = 'A'
df.index.name = 'B'
df
    A   0   1
    B        
    0   9   4
    1  15   0
    2  17  16

it would allow something like

df.rename_columns('A').rename_index('B')

or perhaps be a keyword argument to the existing .rename(). Does this seem like something that would fit in with pandas?

@TomAugspurger
Copy link
Contributor

I've written up a PR for this a couple times, before discarding it, mostly since rename is probably the correct method name and it's not too bad to do with .pipe.

I'm +.5 on adding these methods or something like them. I don't think we should add anything to .rename since that's likely to create confusion.

@jreback
Copy link
Contributor

jreback commented Jan 5, 2016

this is related to #9494

@jreback
Copy link
Contributor

jreback commented Jan 5, 2016

if we implement soln for #10797

then you could do:

df = df.set_index(df.index.set_name('foo'))

(though missing ability to set_columns atm, so only works for index)

@jreback jreback added the Indexing Related to indexing on series/frames, not to indexes themselves label Jan 5, 2016
@TomAugspurger
Copy link
Contributor

I'll put something together for this and #9494

@wcbeard
Copy link
Contributor Author

wcbeard commented Jan 6, 2016

@jreback the df = df.set_index(df.index.set_name('foo')) assignment looks useful, but I don't think it would be reliable in a chain of methods (e.g., after .query or renaming the axes)

Haven't tried it, but I guess another (pretty verbose) strategy would be to use .assign(new_index_name=lambda x: x.index).set_index('new_index_name')

@jreback jreback added this to the 0.18.0 milestone Jan 7, 2016
cldy pushed a commit to cldy/pandas that referenced this issue Feb 11, 2016
closes pandas-dev#9494  closes
pandas-dev#11965    Okay refactored to
overload `NDFrame.rename` and `NDFrame.rename_axis`    New rules:    -
Series.rename(scalar) or Series.rename(list-like) will alter
`Series.name`  - NDFrame.rename(mapping) and NDFrame.rename(function)
operate as before (alter labels)  - NDFrame.rename_axis(scaler) or
NDFrame.rename_axis(list-like) will change the Index.name or
MutliIndex.names  - NDFrame.rename_axis(mapping) and
NDFrame.rename_axis(function) operate as before.    I don't like
overloading the method like this, but `rename` and `rename_axis` are
the right method names. It's *mostly* backwards compatible, aside from
people doing things like `try: series.rename(x): except TypeError:
...` and I don't know what our guarantees are around things like that.

Author: Tom Augspurger <tom.w.augspurger@gmail.com>

Closes pandas-dev#11980 from TomAugspurger/chainable-rename and squashes the following commits:

b36df76 [Tom Augspurger] ENH/API: Series.rename and NDFrame.rename_axis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants