-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH/API: Scalar values for Series.rename and NDFrame.rename_axis #11980
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
Conversation
@@ -367,6 +367,7 @@ Reindexing / Selection / Label manipulation | |||
Series.reindex | |||
Series.reindex_like | |||
Series.rename | |||
Series.set_axis_name | |||
Series.reset_index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add .set_name
I would add an example (like this) maybe in the data structures section. |
Do we need an |
OK, removing inplace. Thoughts on |
that would be quite an API change |
I think it'd be backwards compatible right? Right now we raise a TypeError on scalars / array-likes. I'll writeup an example later. |
6bcd810
to
62bf793
Compare
""" | ||
is_scalar_or_list = ( | ||
(not com.is_sequence(mapper) and not callable(mapper)) or | ||
(com.is_list_like(mapper) and not isinstance(mapper, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a check for is_dict_like
in core/common? I couldn't find one, and can move this or something similar into there if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a is_dict_like
and just make it a check for https://docs.python.org/2/library/collections.html#collections.MutableMapping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use duck typing -- for example, I'm pretty sure Series would fail that test :).
I wrote one of these for xarray: https://github.com/pydata/xarray/blob/v0.7.0/xarray/core/utils.py#L137
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find is_list_like
& co ambiguous, because I don't know if list
means to me what it means to the person who wrote it. the function name should describe the properties being tested for (e.g. is_immuatable_sequence
rather than is_tuple_like
). #12056 is properly the place to discuss that.
62bf793
to
cee54ae
Compare
FYI, changed the API around, see the updated original post, which makes it more like xray Basically we look at the type of the argument. For dict/functions we alter labels as before. For scalar/list-like we alter Series/index name(s). |
cee54ae
to
94e4c86
Compare
Question: what would be the ideal name for the original functionality (i.e. changing the column names/index labels) if we could choose it know? Would that be something else as If we think of something really better, we could also opt to add that as a method, and at least use two different functions in the documentation for these two different operation that we can recommend to use (kind of soft deprecation) |
Further, one drawback of the overloading of rename, is that you cannot rename a certain level of a MultiIndex using a mapping like |
And you're correct not being able to rename a single level of a MultiIndex; you'd have to specify all the levels, even the ones you aren't changing.. We could potentially add a |
note #4160 for long time bug in renaming |
Determin whether ``arg`` is dict-like. | ||
''' | ||
return hasattr(arg, '__getitem__') and hasattr(arg, 'keys') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this work for py3 dictviews? (maybe add a confirming tests in test_common.py)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure what you mean. IIUC the only dictviews are (python3) dict.keys()
, .items()
and .values()
, none of which are dict like.
262aeb3
to
45b1484
Compare
Overloading the APIs of Series.rename and NDFramed.rename_axis for method chaining.
45b1484
to
b36df76
Compare
@jreback could you expand a bit on what you mean by dictviews? I don't think that And on the docstrings, I don't think there's a way to only show certain examples for Series and others for DataFrames, is there? We have other docstrings showing both. I did have add an example with Other than those I think this is ready. The 2.7 build failed while building the docs because the xarray docstring you fixed in 4404d39 |
@TomAugspurger nvm, was thinking of handling the actual |
Ok, thanks! |
side issue: we might want to revisit the signature of
e.g.
|
Think that's worth breaking API for? With At the very least, it's nice that these two methods are at least similar to eachother. They're both for altering the axis labels, and they both take |
actually we also have |
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 <[email protected]> Closes pandas-dev#11980 from TomAugspurger/chainable-rename and squashes the following commits: b36df76 [Tom Augspurger] ENH/API: Series.rename and NDFrame.rename_axis
Closes pandas-dev#12623 I added com.is_dict_like in pandas-dev#11980 and failed to use it for the `rename` method. Using that now and did some refactoring while I was in there. Added more tests for rename.
closes #9494
closes #11965
Okay refactored to overload
NDFrame.rename
andNDFrame.rename_axis
New rules:
Series.name
I don't like overloading the method like this, but
rename
andrename_axis
are the right method names. It's mostly backwards compatible, aside from people doing things liketry: series.rename(x): except TypeError: ...
and I don't know what our guarantees are around things like that.