Python .大熊猫分类指数loc

Python .大熊猫分类指数loc,python,pandas,Python,Pandas,我试图使用.loc访问基于CategoricalIndex的Pandas数据帧的行,但我得到了一个TypeError。最低限度的非工作示例如下 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'

我试图使用.loc访问基于CategoricalIndex的Pandas数据帧的行,但我得到了一个
TypeError
。最低限度的工作示例如下

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')
然后,在尝试访问与标签13对应的行时

df.loc[13]
我明白了

真的
。我知道我最终可以用

df.index.get_loc(13)
但是,为什么上述更简单的方法不起作用?我错过了什么

干杯。

为我工作:

print (df.loc[pd.CategoricalIndex([13])])
              foo
future_index     
13              2
但是如果像前面提到的那样转换成
str
,效果会很好:

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

这看起来像是目前没有为基于整数的分类索引实现的,如果您的分类是基于字符/str的,例如
'a'、'b'、'c'
,那么
df.loc['a']
就可以了。目前看来这是一个限制
print (df.loc[pd.CategoricalIndex([13])])
              foo
future_index     
13              2
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