AttributeError:“DataFrame”对象没有属性“group_by”

Pal*_*ine 2 python dataframe python-3.x python-polars

我正在尝试按文档后面的极坐标数据框进行分组:

https://pola-rs.github.io/polars/py-polars/html/reference/dataframe/api/polars.DataFrame.group_by.html#polars.DataFrame.group_by

import polars as pl
df = pl.DataFrame(
    {
        "a": ["a", "b", "a", "b", "c"],
        "b": [1, 2, 1, 3, 3],
        "c": [5, 4, 3, 2, 1],
    }
)
df.group_by("a").agg(pl.col("b").sum())
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

AttributeError: 'DataFrame' object has no attribute 'group_by'
Run Code Online (Sandbox Code Playgroud)

Pal*_*ine 5

检查后我发现了我的极地版本:

pl.__version__

0.17.3
Run Code Online (Sandbox Code Playgroud)

https://pola-rs.github.io/polars/py-polars/html/reference/dataframe/api/polars.DataFrame.groupby.html

我需要去做:

df.groupby("a").agg(pl.col("b").sum())  # there is no underscore in groupby

#output

shape: (3, 2)
a   b
str i64
"a" 2
"c" 3
"b" 5
Run Code Online (Sandbox Code Playgroud)

该文件说:

自版本 0.19.0 起已弃用:此方法已重命名为DataFrame.group_by().

这是新文件polars version 0.19

https://pola-rs.github.io/polars/py-polars/html/reference/dataframe/api/polars.DataFrame.group_by.html#polars-dataframe-group-by