Skip to content
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

Inconsistent behaviour of rename for MultiIndexed DataFrames and Series #4605

Closed
alefnula opened this issue Aug 19, 2013 · 12 comments · Fixed by #4627

Comments

@alefnula
Copy link
Contributor

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.

@cpcloud

This comment has been minimized.

Copy link
Member

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 jreback referenced this issue Aug 19, 2013
15 of 18 tasks complete
@jreback

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented Aug 19, 2013

maybe we should call it "aftermath"? 😄

@cancan101

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2013

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

@cpcloud

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2013

@cancan101 example?

@cancan101

This comment has been minimized.

@jreback

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2013

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

@cancan101

This comment has been minimized.

Copy link
Contributor

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.

@jreback

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2013

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

@jtratner

This comment has been minimized.

Copy link
Contributor

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.

@jreback

This comment has been minimized.

Copy link
Contributor

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
Projects
None yet
5 participants
You can’t perform that action at this time.