Closed
Description
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.