英文:
Polars - Declare pl.List - TypeError: 'DataTypeClass' object is not subscriptable
问题
我尝试声明一个带有pl.list列的数据框,像这样:
cart_schema = [("CART_ID", pl.Int64), ("ORDERS", pl.List[pl.Int64]), ("TOTAL_DISTANCE", pl.Float64), ("TOTAL_WEIGHT", pl.Float64)]
carts = pl.DataFrame([], schema=cart_schema)
它报错了:TypeError: 'DataTypeClass' object is not subscriptable
我找到的唯一方法是:
carts = pl.DataFrame({'CART_ID':0,'ORDERS':[[0,1]],'TOTAL_DISTANCE':0.0,'TOTAL_WEIGHT':0})
carts = carts.filter(pl.col("CART_ID") != 0)
英文:
I'm trying to declare a dataframe with a column as a pl.list, like that :
cart_schema = [("CART_ID", pl.Int64), ("ORDERS", pl.List[pl.Int64]), ("TOTAL_DISTANCE", pl.Float64), ("TOTAL_WEIGHT", pl.Float64)]
carts = pl.DataFrame([], schema=cart_schema)
It obtains error : TypeError: 'DataTypeClass' object is not subscriptable
Only way, I found to do it is :
carts = pl.DataFrame({'CART_ID':0,'ORDERS':[ [0,1]],'TOTAL_DISTANCE':0.0,'TOTAL_WEIGHT':0 })
carts = carts.filter(pl.col("CART_ID") != 0)
答案1
得分: 1
错误提示如下:Polars - 声明 pl.List - TypeError: 'DataTypeClass' 对象不可订阅。
您正在对 pl.List 进行订阅,即使用 [] 语法,这是不允许的。
相反,您必须通过调用它并使用 () 语法来初始化 pl.List 数据类型:
pl.List(pl.Int64)
英文:
As the error states: Polars - Declare pl.List - TypeError: 'DataTypeClass' object is not subscriptable
You are subscripting - the [] syntax - a pl.List, which is not allowed.
Instead you must initialize a pl.List data type by calling it - using the () syntax -.:
pl.List(pl.Int64)
答案2
得分: 0
如果您正在使用[],您可以尝试使用{}
cart_schema = [
("CART_ID", pl.Int64),
("ORDERS", pl.List(pl.Int64)),
("TOTAL_DISTANCE", pl.Float64),
("TOTAL_WEIGHT", pl.Float64)
]
carts = pl.DataFrame({}, schema=cart_schema)
print(carts)
英文:
If you are using [], Instead you can try using {}
cart_schema= [
("CART_ID", pl.Int64),
("ORDERS", pl.List(pl.Int64)),
("TOTAL_DISTANCE", pl.Float64),
("TOTAL_WEIGHT", pl.Float64)
]
carts = pl.DataFrame({}, schema=cart_schema)
print(carts)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论