Search code examples
pythonpython-3.xpython-polars

Polars alternative solution to using map_groups with with_columns


I think after the recent update of Polars (version 0.20.7), it stopped supporting the use of map_groups with with_columns I got a Type Error stating

TypeError: cannot call `map_groups` when grouping by an expression

I am trying to Group By a column called Product Number and fill the null values using backward strategy for this Group By.

The code I wrote was.

df1 = (
    df1
    .group_by(['Product Number'])
    .map_groups(lambda df1: (
        df1
        .with_columns(
            df1['New Date'].fill_null(strategy='backward').alias('New Date1')
            )
        )
    )
)

Is there alternate solution which supports grouping with with_columns ?


Solution

  • Instead, you can use pl.Expr.over to compute expressions over certain groups.

    (
        df1
        .with_columns(
            pl.col("New Date")
            .fill_null(strategy="backward")
            .over("'Product Number'")
            .alias("New Date1")
        )
    )