Skip to content

IndexSlice cannot infer missing values when an Index object is passed in #16712

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
guocheng opened this issue Jun 16, 2017 · 4 comments · Fixed by #27120
Closed

IndexSlice cannot infer missing values when an Index object is passed in #16712

guocheng opened this issue Jun 16, 2017 · 4 comments · Fixed by #27120
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@guocheng
Copy link

guocheng commented Jun 16, 2017

Here is the code snippet:

import pandas as pd

df = pd.DataFrame({'id': [1,2,3,4,5], 
                   'age':[22,21,23,24,25], 
                   'sex':['M','F','M','F','M'],
                   'height':[165,152,166,154,176]
                  })

df = df.set_index(['id','age','sex'])
i = pd.Index([1,2,3])

df.loc[pd.IndexSlice[i, 21,], ['height']] # TypeError: unhashable type: 'Int64Index'
df.loc[pd.IndexSlice[i, 21,:], ['height']] # works
df.loc[pd.IndexSlice[[1,2,3], 21,], ['height']] # works

If an Index object is passed into the IndexSlice object, it cannot infer missing columns pd.IndexSlice[i, 21,]. A colon has to be used pd.IndexSlice[i, 21,:] or pass an array instead of an Index object [1,2,3]

I don't know if this is intended.

@chris-b1
Copy link
Contributor

I think this should work, in general we seem to unpack anything list or array-like, but isn't happening on this path. PR welcome!

A couple more questionable cases:

In [38]: df.loc[i, :]
TypeError: unhashable type: 'Int64Index'

In [40]: df.loc[pd.IndexSlice[i.values, 21,], ['height']]
TypeError: unhashable type: 'numpy.ndarray'

# strangely, this works
In [39]: df.loc[i.values, :]
Out[39]: 
            height
id age sex        
1  22  M       165
2  21  F       152
3  23  M       166

@chris-b1 chris-b1 added Bug Difficulty Advanced Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jun 19, 2017
@chris-b1 chris-b1 added this to the Next Major Release milestone Jun 19, 2017
@jorisvandenbossche
Copy link
Member

Agree this should work (to regard an Index as list-like, so do the same as if you would replace i with i.tolist(): df.loc[pd.IndexSlice[i.tolist(), 21,], ['height']]).

@memeplex
Copy link

memeplex commented Jul 4, 2017

Even df.loc[i] will fail, it's not necessary to pass any kind of slice. Instead df.loc[[1,2,3]] or df.loc[pd.Series([1,2,3])] work fine.

@TomAugspurger TomAugspurger modified the milestones: 0.21.0, Next Major Release Jul 5, 2017
@toobaz
Copy link
Member

toobaz commented Jun 29, 2019

The bug is fixed, adding test

@toobaz toobaz added the Needs Tests Unit test(s) needed to prevent regressions label Jun 29, 2019
toobaz added a commit to toobaz/pandas that referenced this issue Jun 29, 2019
@jorisvandenbossche jorisvandenbossche modified the milestones: Contributions Welcome, 0.25.0 Jun 29, 2019
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 Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants