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

Chainable Series.set_name() #9494

Closed
wavexx opened this issue Feb 15, 2015 · 11 comments
Closed

Chainable Series.set_name() #9494

wavexx opened this issue Feb 15, 2015 · 11 comments
Labels
API Design Compat pandas objects compatability with Numpy or Python functions
Milestone

Comments

@wavexx
Copy link

wavexx commented Feb 15, 2015

It's sometimes convenient to assign/replace the name of a Series, especially if the result is going to be a DataFrame column, for example:

df.groupby([]).size().set_name('GRP_SIZE').reset_index()

This should be as simple as:

def set_name(self, name):
    self.name = name
    return self

Comments?

@jreback
Copy link
Contributor

jreback commented Feb 16, 2015

this is kind of like #9239 functionaility wise
@TomAugspurger

what about having set_meta(name=...) (maybe in addition to this)?

@jorisvandenbossche

@jreback jreback added API Design Compat pandas objects compatability with Numpy or Python functions labels Feb 16, 2015
@TomAugspurger
Copy link
Contributor

What else would go in set_meta?

And of course, we need to be careful about not modifying the original self.

@jreback
Copy link
Contributor

jreback commented Feb 16, 2015

I think we could move .assign to generic and just have it set the info-items (e.g. columns for frames, items for Panel). I guess then for series it would set elements (e.g. like append). rather than _meta elements (of which name is the only one ATM).

@jreback
Copy link
Contributor

jreback commented Feb 16, 2015

ok, @wavexx want to do a pull-request to add set_name (on Series) only. I think that is fine.

@wavexx
Copy link
Author

wavexx commented Feb 16, 2015

On 02/16/2015 03:03 PM, jreback wrote:

ok, @wavexx want to do a pull-request to add set_name (on Series) only. I think that is fine.

What about modifying self?
Should we return a copy by default and add an inplace argument as well?

@jreback
Copy link
Contributor

jreback commented Feb 16, 2015

I think this needs to copy by default. Though on a series this is quite cheap (as its a view).

@shoyer
Copy link
Member

shoyer commented Feb 17, 2015

Another option is to repurpose Series.rename for this instead of adding a new method -- if the argument is a string (or any hashable?), it renames the Series. This is what I chose to do for xray's equivalent of a Series.

This does overload rename to do two (somewhat) different things (often a sign of poor API design), but rename is the obvious place to look for this functionality.

@jreback
Copy link
Contributor

jreback commented Feb 17, 2015

I would be ok with that!

@shoyer it was my thought about too much o reloading
but I agree it does make sense

and already has the inplace option

@wavexx want to do a PR?

@wavexx
Copy link
Author

wavexx commented Feb 19, 2015

On 02/17/2015 11:54 AM, jreback wrote:

I would be ok with that!

@shoyer it was my thought about too much o reloading
but I agree it does make sense

and already has the inplace option

@wavexx want to do a PR?

I'll give it a shot, but not this week.

@wavexx
Copy link
Author

wavexx commented Feb 11, 2016 via email

@TomAugspurger
Copy link
Contributor

Not a problem. We should have a 0.18 release candidate out soon, if you're willing / able to try it out and report back any bugs!

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 Compat pandas objects compatability with Numpy or Python functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants