我正在尝试基于现有dtype类别的现有列在现有的熊猫数据框中创建有序的类别DType列。我正在运行熊猫0.25.3
我运行了以下代码
print(ri.stop_length.head())
cats = ri['stop_length'].unique()
print(cats)
cattype = CategoricalDtype(categories=cats, ordered=True)
ri['stop_length'] = ri.stop_length.astype('category', cattype)
print(ri.stop_length.cat.ordered)
print(ri.stop_length.head())
这给了我以下输出
stop_datetime
2005-01-04 12:55:00 short
2005-01-23 23:15:00 short
2005-02-17 04:15:00 short
2005-02-20 17:15:00 medium
2005-02-24 01:20:00 short
Name: stop_length, dtype: category
Categories (3, object): [short, medium, long]
[short, medium, long]
Categories (3, object): [short, medium, long]
False
stop_datetime
2005-01-04 12:55:00 short
2005-01-23 23:15:00 short
2005-02-17 04:15:00 short
2005-02-20 17:15:00 medium
2005-02-24 01:20:00 short
Name: stop_length, dtype: category
Categories (3, object): [short, medium, long]
为什么没有得到分类的dtype?
要检查,我已经跑步
ri[ri.stop_length > 'short'].shape
返回以下错误
TypeError:无序分类只能比较相等与否