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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With