Skip to content

BUG: Unexpected behavior from DataFrame.index.levels #7614

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
8one6 opened this issue Jun 30, 2014 · 4 comments
Closed

BUG: Unexpected behavior from DataFrame.index.levels #7614

8one6 opened this issue Jun 30, 2014 · 4 comments

Comments

@8one6
Copy link

8one6 commented Jun 30, 2014

Perhaps this is me misunderstanding the way Pandas handles indices for related objects behind the scenes, but I found the following behavior very unintuitive:

import pandas as pd
import numpy as np

np.random.seed(0)

idx = pd.MultiIndex.from_product([['John', 'Josh', 'Alex'], list('abcde')], 
                                 names=['Person', 'Letter'])
large = pd.DataFrame(data=np.random.randn(15, 2), 
                     index=idx, 
                     columns=['one', 'two'])
small = large.loc[['Jo'==d[0:2] for d in large.index.get_level_values('Person')]]

print small.index.levels[0]
print large.index.levels[0]

This returns:

Index([u'Alex', u'John', u'Josh'], dtype='object')
Index([u'Alex', u'John', u'Josh'], dtype='object')

rather than the expected

Index([u'John', u'Josh'], dtype='object')
Index([u'Alex', u'John', u'Josh'], dtype='object')

I could get the results I expected by running

small.index.get_level_values('Person').unique()
large.index.get_level_values('Person').unique()

but could someone explain why the behavior I'm seeing above with DataFrame.index.levels is an appropriate result rather than a bug?

@jreback
Copy link
Contributor

jreback commented Jun 30, 2014

see discussion here:

#2770

@8one6
Copy link
Author

8one6 commented Jul 2, 2014

Would it make sense to say somewhere in the docs that the .unique() method can be run on the kind of data you get back from calling the .get_level_values(levelname) method?

@jreback
Copy link
Contributor

jreback commented Jul 2, 2014

.get_level_values() returns an Index, and unique() is a method of an Index. So I would expect this to be natural. if you think its unclear, pls submit a pull-request for a doc update.

@jreback
Copy link
Contributor

jreback commented Oct 20, 2015

xref to #2770

@jreback jreback closed this as completed Oct 20, 2015
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

No branches or pull requests

2 participants