教程

polar expression

polar.all() vs. Expr.all() 两者作用不同

  1. polar.all(): 选择所有列
  2. Expr.all(): expression 的一个方法,逻辑判断全为 true, 类似 python 内建函数 all

polar.flatten() 类似 pd.Series.explode()

作用:

  • 拆分单元格中的列表元素到不同的新行

polar namespace 概念

类别:

pl.read_csv()

dtype 错误

解决办法:

  1. 手动指定: dtypes={'a_col': pl.Int64, ...}
  2. 调整 infer_schema_length = num 一个较大的数值

    • 多次调整 num, 直到成功位置
  3. 设定推断类似涉及的行数为 0: pl.read_csv(..., infer_schema_length=0)

    • 设 infer_schema_length=0, pl.read_csv 把所有列读取成 pl.Utf8
    • 读取完成后再手动调整类型
    • eg:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      
      import polars as pl
      
      
      (pl.read_csv("test.csv", infer_schema_length=0)
         .with_columns(
             [
                 pl.select('a').cast(pl.Uint8, strict=True)
             ]
      
         )

filter

过滤条件连接使用 "&"

1
df.filter((pl.col("id") <= 2) & (pl.col("size") == "small"))