-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Implement dict-like support for rename and set_names in MultiIndex #38126
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
@@ -1366,6 +1367,12 @@ def set_names(self, names, level=None, inplace: bool = False): | |||
( 'cobra', 2018), | |||
( 'cobra', 2019)], | |||
names=['species', 'year']) | |||
>>> idx.set_names({'kind': 'snake'}) |
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 a comment on this example (skip a line)
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.
Done
pandas/core/indexes/base.py
Outdated
@@ -1385,6 +1398,12 @@ def set_names(self, names, level=None, inplace: bool = False): | |||
idx = self | |||
else: | |||
idx = self._shallow_copy() | |||
|
|||
if isinstance(self, ABCMultiIndex) and is_dict_like(names): |
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 a comment on what this condition is doing?
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.
Done
pandas/core/indexes/base.py
Outdated
@@ -1385,6 +1398,12 @@ def set_names(self, names, level=None, inplace: bool = False): | |||
idx = self | |||
else: | |||
idx = self._shallow_copy() | |||
|
|||
if isinstance(self, ABCMultiIndex) and is_dict_like(names): | |||
level = Index(self.names).get_indexer_for(names) |
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.
can you not just iterate over these to grab the levels? (and use get_level_values)?
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.
Yep, thx. Thats way better.
List comprehensions would be an option too, but would have to loop twice. Would prefer ist this way
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.
see comments
pandas/core/indexes/base.py
Outdated
@@ -1376,6 +1393,12 @@ def set_names(self, names, level=None, inplace: bool = False): | |||
if not is_list_like(names) and level is None and self.nlevels > 1: | |||
raise TypeError("Must pass list-like as `names`.") | |||
|
|||
if is_dict_like(names) and not isinstance(self, ABCMultiIndex): |
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.
all of these conditions can be if/elif
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.
Wanted to be consistent with the existing conditions, changed them to elif
|
||
if is_dict_like(names) and level is not None: | ||
raise TypeError("Can not pass level when passing dict-like as `names`.") | ||
|
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.
doesn't this hit a dict-like names?
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.
Yikes, yes you are right. have not though about dict like names. Removed this
pandas/core/indexes/base.py
Outdated
if isinstance(self, ABCMultiIndex) and is_dict_like(names): | ||
# Transform dict to list of new names and corresponding levels | ||
level, names_adjusted = [], [] | ||
for i, name in enumerate(self.names): |
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.
see my comment above i think this condition needs to be on L1402
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.
Moved it up a bit and added level is None
cc @Dr-Irv if you'd have a look. this is api you were expecting (from the OP)? |
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.
Needs tests for the basic use case in the original issue.
Yes, that is the API. I didn't check the implementation, but the tests should reflect the "normal" use case of having a |
Concerning missing names: I have oriented me on df.rename, as you also suggested. df.rename does nothing for missing index elements or column names. Additionally for duplicates: Same holds true here. Is also done like df.rename. It produces duplicates if we have the same name twice in the original and it also produces duplicates if the targets already exist. |
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.
LGTM. Final approval rests with @jreback
…421_before_merge � Conflicts: � doc/source/whatsnew/v1.2.0.rst
…421_before_merge � Conflicts: � doc/source/whatsnew/v1.3.0.rst
ping |
pandas/core/indexes/base.py
Outdated
If the index is a MultiIndex and names is not dict-like, level(s) to set | ||
(None for all levels). Otherwise level must be None. | ||
|
||
.. versionchanged:: 1.2.0 |
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.
1.3
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.
Done
pandas/core/indexes/base.py
Outdated
@@ -1425,16 +1433,37 @@ def set_names(self, names, level=None, inplace: bool = False): | |||
( 'cobra', 2018), | |||
( 'cobra', 2019)], | |||
names=['species', 'year']) | |||
|
|||
When renaming levels through a dictionary no level can't be passed. |
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.
When renaming levels with a dict, levels can not be passed.
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.
Done
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.
comment, ping on green
pandas/core/indexes/base.py
Outdated
Name(s) to set. | ||
|
||
.. versionchanged:: 1.2.0 |
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.
change to 1.3
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.
Thx
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.
Done
@jreback green |
thanks @phofl |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
I tried my hand in adding support for dict-like renamings for MultiIndexes. This seemed as the most straight forward way to me, because we do not have to add complex logic as long as we can cast the dict-like objects to regular configurations.
We probably must add a docstring to MultiIndex.rename now?