Skip to content

Inconsistent behaviour of rename for MultiIndexed DataFrames and Series #4605

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
alefnula opened this issue Aug 19, 2013 · 12 comments · Fixed by #4627
Closed

Inconsistent behaviour of rename for MultiIndexed DataFrames and Series #4605

alefnula opened this issue Aug 19, 2013 · 12 comments · Fixed by #4627
Labels
API Design Bug Internals Related to non-user accessible pandas implementation
Milestone

Comments

@alefnula
Copy link
Contributor

If I try to rename a MultiIndexed DataFrame, everything works correctly:

In [3]: pd.DataFrame([11,21,31],
            index=pd.MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]])).rename(str.lower)
Out[3]: 
      0
a a  11
  b  21
  c  31

But if I do the same thing with the Series, it doesn't work:

In [4]: pd.Series([11,21,31],
            index=pd.MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]])).rename(str.lower)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-aa25b537c1df> in <module>()
----> 1 pd.Series([11,21,31],index=pd.MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]])).rename(str.lower)

/home/viktor/pandas/pandas/core/series.py in rename(self, mapper, inplace)
   3187         result = self if inplace else self.copy()
   3188         result.index = Index([mapper_f(x)
-> 3189                              for x in self.index], name=self.index.name)
   3190 
   3191         if not inplace:

TypeError: descriptor 'lower' requires a 'str' object but received a 'tuple'

This behavior is observed in 0.12 and in the master branch.

@cpcloud
Copy link
Member

cpcloud commented Aug 19, 2013

And here's the SO question.

looks like it should be called on every element of the tuple similar to frames. not sure why it's not

@jreback
Copy link
Contributor

jreback commented Aug 19, 2013

yep..think this has always been there, adding to the post series2 cleaning; another inconsistency between series/frame....should be fixable (though these are not always trivial as series jumped thru hoops sometimes to do some of these types of things)

@cpcloud
Copy link
Member

cpcloud commented Aug 19, 2013

maybe we should call it "aftermath"? 😄

@cancan101
Copy link
Contributor

Another small observation: DataFrame take an index kwarg whereas Series does not.

@cpcloud
Copy link
Member

cpcloud commented Aug 19, 2013

That's not true. Do Series?? in IPython. It's possible that there's an inconsistency in the documentation somewhere though.

@jreback
Copy link
Contributor

jreback commented Aug 19, 2013

@cancan101 example?

@cancan101
Copy link
Contributor

@jreback
Copy link
Contributor

jreback commented Aug 19, 2013

I suppose instead of the mapper argument it could/should be called index? (for consistency)

@cancan101
Copy link
Contributor

Yes. Although for backward compatibility I would imagine Series.rename will need to take both and assert that only one is set.

@jreback
Copy link
Contributor

jreback commented Aug 19, 2013

ok....marking as an API issue (and a bug), will look at some point

@jtratner
Copy link
Contributor

it probably just needs to special case MultiIndex, since if you look at the
code snippet, it's assuming that it's an Index instead.

@jreback
Copy link
Contributor

jreback commented Aug 19, 2013

Series.rename just needs to combine with DataFrame.rename/Panel.rename and be put in core/generic.py pretty straightforward (I put it on the todo for after series2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Internals Related to non-user accessible pandas implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants