Skip to content

BUG: Fix the rename function for Series and DataFrame, #3165 #3175

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

Merged
merged 1 commit into from
Mar 25, 2013

Conversation

waitingkuo
Copy link
Contributor

Bug 1: After rename, the name of the index is missing:

In [1]: import pandas as pd

In [2]: data = [1, 2]

In [3]: index = pd.Index(['a', 'b'], name='name')

In [4]: df = pd.DataFrame(data, index=index)

In [5]: df 
Out[5]: 
      0
name   
a     1
b     2

In [6]: df.rename({'a': 'c'})
Out[6]: 
   0
c  1
b  2

Both Series and DataFrame has the same problems. The reason is that we didn't set the name of the index while reconstructing the DataFrame/Series by rename .

Bug 2: rename cannot work for the MultiIndex case, the index becomes tuples, the names of the index are gone

In [1]: import pandas as pd

In [2]: data = [1, 2]

In [3]: index = pd.MultiIndex.from_tuples([('a', 'b'), ('c', 'd')], names=['name1', 'name2'])

In [4]: df = pd.DataFrame(data, index=index)

In [5]: df
Out[5]: 
             0
name1 name2   
a     b      1
c     d      2

In [6]: df.rename({'a': 'e'}) 
Out[6]: 
        0
(a, b)  1
(c, d)  2

The missing name issue is same as Bug1. The original rename function convert the dict to the mapper function and map each tuple but not each index, and doesn't reconstruct it as MultiIndex, so it becomes a Index with some tuples.

Fix

I've fixed the above two bugs, the rename will also pass the name of the index too, the rename mapper will go through each index for the MultiIndex case now.
I also added some test cases in test_series.py and test_frame.py

@hayd
Copy link
Contributor

hayd commented Mar 25, 2013

This looks great!

@jreback
Copy link
Contributor

jreback commented Mar 25, 2013

@waitingkuo you beat me too it! looks good

@waitingkuo
Copy link
Contributor Author

Hope this might help : )

@waitingkuo waitingkuo closed this Mar 25, 2013
@waitingkuo waitingkuo reopened this Mar 25, 2013
hayd added a commit that referenced this pull request Mar 25, 2013
BUG: Fix the rename function for Series and DataFrame, #3165
@hayd hayd merged commit e1e14a4 into pandas-dev:master Mar 25, 2013
@waitingkuo waitingkuo deleted the fix-rename branch March 25, 2013 19:46
@ghost
Copy link

ghost commented Mar 25, 2013

Please add an entry to RELEASE.rst

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants