Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.loc for CategoricalIndex in Pandas

Tags:

python

pandas

I'm trying to access the rows of a CategoricalIndex-based Pandas dataframe using .loc but I get a TypeError. A minimum non working example would be

import pandas as pd

df = pd.DataFrame({'foo': rand(3), 'future_index': [22, 13, 87]})
df['future_index'] = df['future_index'].astype('category')
df = df.set_index('future_index')

Then, in trying to access the row corresponding to label 13 as

df.loc[13]

I get

TypeError: cannot do label indexing on <class 'pandas.core.indexes.category.CategoricalIndex'> with these indexers [13] of <class 'int'>

despite

13 in df.index

being True. I know I can ultimately obtain the index of 13 with

df.index.get_loc(13)

but, why is the above easier approach not working? What am I missing?

Cheers.

like image 519
manu Avatar asked Oct 18 '22 06:10

manu


1 Answers

For me working:

print (df.loc[pd.CategoricalIndex([13])])
              foo
future_index     
13              2

But if convert to str as mentioned EdChum it working nice:

df = pd.DataFrame({'foo': [1,2,3], 'future_index': [22, 13, 87]})
df['future_index'] = df['future_index'].astype(str).astype('category')
df = df.set_index('future_index')
print (df)

print (df.loc['13'])
foo    2
Name: 13, dtype: int64
like image 82
jezrael Avatar answered Oct 21 '22 00:10

jezrael