python - 如何在分类类型上设置索引?
问题描述
鉴于此 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
解决方案
分类对象必须先定义有序,然后才能被索引。错误消息告诉我们使用as_ordered()
. 这个方法来自于cat
结构:
df['symbol'] = df['symbol'].cat.as_ordered()
df = df.set_index('symbol')
推荐阅读
- reactjs - 可以在 jsx 中使用的道具的正确类型是什么?
- python - 无法在 pyspark 中将 JSON 转换为 StructField
- amazon-cloudformation - 如何使用 cloudformation 将资源从一个 yaml 模板调用到另一个 yaml 模板
- java - 即使为 SSLContext 配置了接受所有证书的 TrustManager,Weblogic 也会使用配置的密钥库
- python - 为什么可以将文件作为字符串传递给 open 函数
- c# - Blazor Valued 已更改,同时保持 2 向绑定
- opencv - 计算像素深度
- amazon-web-services - 通过 SQS lambda 发送 SQS 消息
- serverless-framework - 避免使用无服务器创建新资源(如果存在)
- html - 面板主体引导程序中的水平滚动条