Inconsistent behaviour of rename for MultiIndexed DataFrames and Series #4605

Closed
alefnula opened this Issue Aug 19, 2013 · 12 comments

Comments

Projects
None yet
5 participants
Contributor

alefnula commented Aug 19, 2013

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.

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 referenced this issue Aug 19, 2013

Closed

CLN: Post Series subclass from NDFrame #4324

15 of 18 tasks complete
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)

Member

cpcloud commented Aug 19, 2013

maybe we should call it "aftermath"? 😄

Contributor

cancan101 commented Aug 19, 2013

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

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.

Contributor

jreback commented Aug 19, 2013

@cancan101 example?

Contributor

jreback commented Aug 19, 2013

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

Contributor

cancan101 commented Aug 19, 2013

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

Contributor

jreback commented Aug 19, 2013

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

Contributor

jtratner commented Aug 19, 2013

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.

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)

jreback closed this in #4627 Aug 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment