如何设置分类类型的索引?

时间:2018-11-24 17:02:12

标签: python pandas dataframe dask

给出这个Dask DataFrame:

Dask DataFrame Structure:
             date  value           symbol
npartitions=2                                
           object  int64  category[known]
...              ...
...              ...
Dask Name: from-delayed, 6 tasks2130

如何在“符号”列(类别为[已知])上设置索引?

df = df.set_index('symbol')
Traceback (most recent call last):
[...]
TypeError: Categorical is not ordered for operation max
you can use .as_ordered() to change the Categorical to an ordered one

1 个答案:

答案 0 :(得分:2)

必须对分类对象进行有序定义,然后才能对其建立索引。错误消息告诉我们使用as_ordered()。此方法来自cat结构:

df['symbol'] = df['symbol'].cat.as_ordered()
df = df.set_index('symbol')