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

BUG: DataFrame.loc with a MultiIndex does not always collapse scalar levels #10552

Open
shoyer opened this issue Jul 11, 2015 · 3 comments
Open
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@shoyer
Copy link
Member

shoyer commented Jul 11, 2015

All of the following should be equivalent:

In [21]: df = pd.DataFrame({'a': [1], 'b': [2], 'c': [3]}).set_index(['a', 'b'])

In [22]: df.loc[1]
Out[22]:
   c
b
2  3

In [23]: df.loc[(1,), slice(None)]
Out[23]:
   c
b
2  3

In [24]: df.loc[(1, slice(None)), slice(None)]
Out[24]:
     c
a b
1 2  3

In the last output, there should no longer be a level a.

@shoyer shoyer added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 11, 2015
@jreback
Copy link
Contributor

jreback commented Jul 12, 2015

The prior impl made this hard to do, but was just refactored so I think it could be fixed up to give the proper semantics (e.g. scalar vs. list-like).

@jorisvandenbossche
Copy link
Member

Just to be clear, the general rule we want is: "if the level indexer is a scalar, collapse the level, otherwise keep the level". Correct?

This should then also hold for levels that are not up front?

So this

In [42]: df.loc[(slice(None), 2), :]
Out[42]:
     c
a b
1 2  3

would give

So

In [42]: df.loc[(slice(None), 2), :]
Out[42]:
   c
a
1  3

@jreback
Copy link
Contributor

jreback commented Jul 14, 2015

currently this is ONLY implemented for .xs and .loc with the axes (not multi-levels), though may work for a single multi-level as per the impl. I think @jorisvandenbossche is prob right, should squeeze scalars; this can be done as a post-processing step in .get_loc, so straightforward I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

5 participants