Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set index on categorical type?

Given this Dask DataFrame :

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

How can I set_index on 'symbol' column (which is category[known)?

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
like image 265
Ghislain Viguier Avatar asked Nov 24 '18 17:11

Ghislain Viguier


1 Answers

Categorical objects must be defined ordered before they can be indexed. The error message tells us to use the as_ordered(). This method comes from the cat structure:

df['symbol'] = df['symbol'].cat.as_ordered()
df = df.set_index('symbol')
like image 94
Ghislain Viguier Avatar answered Oct 05 '22 14:10

Ghislain Viguier