首页 > 解决方案 > 如何在分类类型上设置索引?

问题描述

鉴于此 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

标签: pythonpandasdataframedask

解决方案


分类对象必须先定义有序,然后才能被索引。错误消息告诉我们使用as_ordered(). 这个方法来自于cat结构:

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

推荐阅读