pola-rs/polars · error · ValueError

use of `aggregate_function='count'` should be replaced with

Error message

use of `aggregate_function='count'` should be replaced with `aggregate_function='len'`.

What it means

Polars' LazyFrame.rolling/aggregation API accepts aggregate_function='len' to count rows, but 'count' was removed/deprecated because it historically was ambiguous (count of rows vs non-null values). Passing 'count' now raises a ValueError directing you to use 'len' (or 'sum' etc. for value aggregations).

Source

Thrown at py-polars/src/polars/lazyframe/frame.py:8234

            elif aggregate_function == "item":
                agg = agg.item()
            elif aggregate_function == "sum":
                agg = agg.sum()
            elif aggregate_function == "max":
                agg = agg.max()
            elif aggregate_function == "min":
                agg = agg.min()
            elif aggregate_function == "mean":
                agg = agg.mean()
            elif aggregate_function == "median":
                agg = agg.median()
            elif aggregate_function == "last":
                agg = agg.last()
            elif aggregate_function == "len":
                agg = agg.len()
            elif aggregate_function == "count":
                msg = "use of `aggregate_function='count'` should be replaced with `aggregate_function='len'`."
                raise ValueError(msg)
            else:
                msg = f"invalid input for `aggregate_function` argument: {aggregate_function!r}"
                raise ValueError(msg)
        elif aggregate_function is None:
            agg = agg.item(allow_empty=True)
        else:
            agg = aggregate_function

        on_cols: pl.DataFrame
        if isinstance(on_columns, pl.DataFrame):
            on_cols = on_columns
        elif isinstance(on_columns, pl.Series):
            on_cols = on_columns.to_frame()
        elif isinstance(on_columns, str):
            msg = f"invalid type for `on_columns` argument: {qualified_type_name(on_columns)!r}"
            raise TypeError(msg)
        else:
            on_cols = pl.Series(values=on_columns).to_frame()

View on GitHub (pinned to 68506541d2)

Solutions

  1. Replace aggregate_function='count' with aggregate_function='len'
  2. If you need non-null value counts, use aggregate_function='sum' on a non-null indicator or an explicit .count() expression after the aggregation
  3. Pin/upgrade Polars deliberately and grep the codebase for aggregate_function="count" during migration

Example fix

# before
lf.rolling(index_column='ts', period='1d', aggregate_function='count')

# after
lf.rolling(index_column='ts', period='1d', aggregate_function='len')
Defensive patterns

Strategy: validation

Validate before calling

allowed = {'min','max','first','last','sum','mean','median','len','std','var'}
assert aggregate_function in allowed, f"unsupported aggregate_function: {aggregate_function!r}"

Prevention

When it happens

Trigger: Calling an aggregation API (e.g. LazyFrame.rolling or group-by style helper in frame.py around line 8141) with the string aggregate_function='count'.

Common situations: Code written for older Polars versions where 'count' was accepted; copy-pasted StackOverflow answers; migrating pipelines after a Polars upgrade where 'count' was deprecated then turned into a hard error.

Related errors


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-28). Data as JSON: /api/errors/0f9a1e16e9f55257. Report an issue: GitHub.