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

Deprecation guard in LazyFrame.group_by/agg handling: aggregate_function='count' was renamed to 'len'; passing the removed value raises immediately, pointing to the replacement, instead of behaving like 'len'.

Solutions

  1. Replace `aggregate_function="count"` with `aggregate_function="len"`.
  2. Search the codebase for `aggregate_function` occurrences and audit string values.
  3. Upgrade polars and address deprecation warnings before they become hard errors.

Example fix

// before
lf.pivot(on="date", index="id", values="sales", aggregate_function="count")
// after
lf.pivot(on="date", index="id", values="sales", aggregate_function="len")
Defensive patterns

Strategy: validation

Validate before calling

if aggregate_function == "count":
    aggregate_function = "len"

Try / catch

try:
    out = lf.pivot(on=on, index=idx, values=vals, aggregate_function=agg)
except ValueError as e:
    if "aggregate_function='count'" in str(e):
        out = lf.pivot(on=on, index=idx, values=vals, aggregate_function="len")
    else:
        raise

Prevention

When it happens

Trigger: Calling `lf.pivot(on=..., index=..., values=..., aggregate_function="count")` — the string comparison branch raises before execution.

Common situations: Migrating from older polars versions where 'count' was accepted; copying pivot snippets from old blog posts or pandas-style aggregation names.

Understand the failure class

Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/0f9a1e16e9f55257. Report an issue: GitHub.

Appendix: source

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

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