Search code examples
pythonpython-3.xdataframepython-polars

AttributeError: 'DataFrame' object has no attribute 'group_by'


I am trying to group by a polars dataframe following the document:

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())

However, I am getting this error:

AttributeError: 'DataFrame' object has no attribute 'group_by'

Solution

  • On checking I found my polars version :

    pl.__version__
    
    0.17.3
    

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

    I need to do:

    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
    

    and the document says :

    Deprecated since version 0.19.0: This method has been renamed to DataFrame.group_by().

    This is the new document for 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