Polars – 声明 pl.List – 类型错误: ‘DataTypeClass’ 对象不可索引

huangapple go评论99阅读模式
英文:

Polars - Declare pl.List - TypeError: 'DataTypeClass' object is not subscriptable

问题

我尝试声明一个带有pl.list列的数据框,像这样:

  1. cart_schema = [("CART_ID", pl.Int64), ("ORDERS", pl.List[pl.Int64]), ("TOTAL_DISTANCE", pl.Float64), ("TOTAL_WEIGHT", pl.Float64)]
  2. carts = pl.DataFrame([], schema=cart_schema)

它报错了:TypeError: 'DataTypeClass' object is not subscriptable

我找到的唯一方法是:

  1. carts = pl.DataFrame({'CART_ID':0,'ORDERS':[[0,1]],'TOTAL_DISTANCE':0.0,'TOTAL_WEIGHT':0})
  2. carts = carts.filter(pl.col("CART_ID") != 0)
英文:

I'm trying to declare a dataframe with a column as a pl.list, like that :

  1. cart_schema = [("CART_ID", pl.Int64), ("ORDERS", pl.List[pl.Int64]), ("TOTAL_DISTANCE", pl.Float64), ("TOTAL_WEIGHT", pl.Float64)]
  2. carts = pl.DataFrame([], schema=cart_schema)

It obtains error : TypeError: 'DataTypeClass' object is not subscriptable

Only way, I found to do it is :

  1. carts = pl.DataFrame({'CART_ID':0,'ORDERS':[ [0,1]],'TOTAL_DISTANCE':0.0,'TOTAL_WEIGHT':0 })
  2. carts = carts.filter(pl.col("CART_ID") != 0)

答案1

得分: 1

错误提示如下:Polars - 声明 pl.List - TypeError: 'DataTypeClass' 对象不可订阅

您正在对 pl.List 进行订阅,即使用 [] 语法,这是不允许的。

相反,您必须通过调用它并使用 () 语法来初始化 pl.List 数据类型:

  1. 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 -.:

  1. pl.List(pl.Int64)

答案2

得分: 0

如果您正在使用[],您可以尝试使用{}

  1. cart_schema = [
  2. ("CART_ID", pl.Int64),
  3. ("ORDERS", pl.List(pl.Int64)),
  4. ("TOTAL_DISTANCE", pl.Float64),
  5. ("TOTAL_WEIGHT", pl.Float64)
  6. ]
  7. carts = pl.DataFrame({}, schema=cart_schema)
  8. print(carts)
英文:

If you are using [], Instead you can try using {}

  1. cart_schema= [
  2. ("CART_ID", pl.Int64),
  3. ("ORDERS", pl.List(pl.Int64)),
  4. ("TOTAL_DISTANCE", pl.Float64),
  5. ("TOTAL_WEIGHT", pl.Float64)
  6. ]
  7. carts = pl.DataFrame({}, schema=cart_schema)
  8. print(carts)

huangapple
  • 本文由 发表于 2023年5月29日 17:25:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定