Skip to content

Rename in MultiIndex flattens index #3165

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

Closed
hayd opened this issue Mar 25, 2013 · 2 comments · Fixed by #4004
Closed

Rename in MultiIndex flattens index #3165

hayd opened this issue Mar 25, 2013 · 2 comments · Fixed by #4004
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@hayd
Copy link
Contributor

hayd commented Mar 25, 2013

When trying to rename something in a MultiIndex it doesn't work and flattens the index:

df = pd.DataFrame({'Data1': {0: 'present', 1: 'absent'}, 'Data2': {0: 'absent', 1: 'present'}, 'Location': {0: 'loc1', 1: 'loc2'}, 'Name': {0: 'Foo', 1: 'Foo2'}, 'Position': {0: 12345, 1: 67890}}).set_index(['Name', 'Location', 'Position'])

In [2]: df
Out[2]: 
                          Data1    Data2
Name Location Position                  
Foo  loc1     12345     present   absent
Foo2 loc2     67890      absent  present

In [3]: df.rename({'Foo2': 'Bar'})
Out[3]: 
                       Data1    Data2
(Foo, loc1, 12345)   present   absent
(Foo2, loc2, 67890)   absent  present

In [4]: df.rename_axis({'Foo2': 'Bar'}, axis=1)
Out[4]: 
                       Data1    Data2
(Foo, loc1, 12345)   present   absent
(Foo2, loc2, 67890)   absent  present

Taken from this SO question.

@waitingkuo
Copy link
Contributor

Seems the name of the index is gone after rename for both Series and DataFrame

In [1]: import pandas as pd
In [2]: sr = pd.Series([1, 2, 3], pd.Index(['a', 'b', 'c'], name='test'))

In [3]: sr
Out[4]: 
test
a       1
b       2
c       3

In [4]: sr.rename({'a': 'd'})
Out[4]: 
d    1
b    2
c    3

@hayd
Copy link
Contributor Author

hayd commented Mar 25, 2013

I've added it to my list of "things to fix with index/column names" :)

waitingkuo added a commit to waitingkuo/pandas that referenced this issue Mar 25, 2013
hayd added a commit that referenced this issue Mar 25, 2013
BUG: Fix the rename function for Series and DataFrame, #3165
@hayd hayd closed this as completed Mar 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants