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

Fix shape attribute for MultiIndex #8609

Merged
merged 1 commit into from
Oct 24, 2014

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Oct 23, 2014

This fix ensures that idx.shape == (len(idx),) if idx is a MultiIndex.

Also added tests for other ndarray compat properties.

@jreback
Copy link
Contributor

jreback commented Oct 23, 2014

hmm these should be the same
is something failing?

@shoyer
Copy link
Member Author

shoyer commented Oct 23, 2014

@jreback here's what I found on master:

In [1]: import pandas as pd

In [2]: idx = pd.MultiIndex.from_arrays([['a', 'b'], [0, 1]])

In [3]: idx
Out[3]:
MultiIndex(levels=[[u'a', u'b'], [0, 1]],
           labels=[[0, 1], [0, 1]])

In [4]: idx.shape
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-f9305225184b> in <module>()
----> 1 idx.shape

/Users/shoyer/dev/pandas/pandas/core/base.pyc in shape(self)
    298     def shape(self):
    299         """ return a tuple of the shape of the underlying data """
--> 300         return self._data.shape
    301
    302     @property

AttributeError: 'NoneType' object has no attribute 'shape'

@jreback
Copy link
Contributor

jreback commented Oct 24, 2014

this is a little odd because a MultiIndex is 1-d. So this could be a bit confusing. len(idx) == shape(idx)[0]

@shoyer
Copy link
Member Author

shoyer commented Oct 24, 2014

IMO MultiIndex can be unambiguously interpreted as a 1d array of tuples. I don't think it's surprising to have len(idx) == shape(idx)[0] -- that's exactly the case for any 1d numpy array. All the other indexes already have shape defined (in the same way).

@jreback
Copy link
Contributor

jreback commented Oct 24, 2014

I know but you want to return (5,20) or something right? (e.g. tuple of length of levels)?
maybe could make a new attribute? thought this prob doesn't break anything

@shoyer
Copy link
Member Author

shoyer commented Oct 24, 2014

Nope, I just want to return (len(idx),)

On Fri, Oct 24, 2014 at 12:04 PM, jreback notifications@github.com
wrote:

I know but you want to return (5,20) or something right? (e.g. tuple of length of levels)?

maybe could make a new attribute? thought this prob doesn't break anything

Reply to this email directly or view it on GitHub:
#8609 (comment)

@jreback jreback added this to the 0.15.1 milestone Oct 24, 2014
@jreback
Copy link
Contributor

jreback commented Oct 24, 2014

ahh, then this is indeed a missing component. ok, rebase and will put in

Also added tests for other ndarray compat properties.
@shoyer
Copy link
Member Author

shoyer commented Oct 24, 2014

rebased and green

jreback added a commit that referenced this pull request Oct 24, 2014
Fix shape attribute for MultiIndex
@jreback jreback merged commit 85069eb into pandas-dev:master Oct 24, 2014
@shoyer shoyer deleted the MultiIndex-shape branch October 25, 2014 00:52
@jreback jreback modified the milestones: 0.15.2, 0.15.1 Oct 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants