Skip to content

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

Closed
@guocheng

Description

@guocheng

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndexNeeds TestsUnit test(s) needed to prevent regressions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions