pola-rs/polars · error · TypeError

specifying aggregations as a dictionary is not…

Error message

specifying aggregations as a dictionary is not supported

Try unpacking the dictionary to take advantage of the keyword syntax of the `agg` method.

What it means

API rejection in LazyFrame.agg: dictionary-style aggregations (as supported by pandas/older APIs) are not accepted; the first aggs element being a dict triggers this error telling the caller to pass keyword-expression aggregations instead.

Solutions

  1. Unpack the dict into keyword/expression syntax: agg(pl.col("a").sum(), pl.col("b").mean())
  2. Use keyword args for named outputs: agg(total=pl.col("a").sum())
  3. Build a list of expressions programmatically and unpack: agg(*exprs)

Example fix

// before
df.group_by("g").agg({"a": "sum", "b": "mean"})
// after
df.group_by("g").agg(pl.col("a").sum(), pl.col("b").mean())
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(aggs, dict):
    aggs = [pl.col(k).__getattribute__(v)() if isinstance(v, str) else v.alias(k) for k, v in aggs.items()]
df.group_by("g").agg(*aggs)

Type guard

def is_dict_agg(x): return isinstance(x, dict)
# convert before calling agg if True

Try / catch

try:
    lf.agg(aggs)
except TypeError:
    lf.agg(**aggs)  # if values are expression-compatible

Prevention

When it happens

Trigger: Calling df.group_by(...).agg({"a": "sum", "b": "mean"}) or passing a dict variable as the first positional argument to agg.

Common situations: Migrating pandas code or older polars (<0.10) code that used the dict syntax; dynamically building aggregations into a dict.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/lazyframe/group_by.py:187

        ... ).collect()  # doctest: +IGNORE_RESULT
        shape: (3, 3)
        ┌─────┬───────┬────────────────┐
        │ a   ┆ b_sum ┆ c_mean_squared │
        │ --- ┆ ---   ┆ ---            │
        │ str ┆ i64   ┆ f64            │
        ╞═════╪═══════╪════════════════╡
        │ a   ┆ 2     ┆ 17.0           │
        │ c   ┆ 3     ┆ 1.0            │
        │ b   ┆ 5     ┆ 10.0           │
        └─────┴───────┴────────────────┘
        """
        if aggs and isinstance(aggs[0], dict):
            msg = (
                "specifying aggregations as a dictionary is not supported"
                "\n\nTry unpacking the dictionary to take advantage of the keyword syntax"
                " of the `agg` method."
            )
            raise TypeError(msg)

        pyexprs = parse_into_list_of_expressions(*aggs, **named_aggs)
        return wrap_ldf(self.lgb.agg(pyexprs))

    def map_groups(
        self,
        function: Callable[[DataFrame], DataFrame],
        schema: SchemaDict | None,
    ) -> LazyFrame:
        """
        Apply a custom/user-defined function (UDF) over the groups as a new DataFrame.

        .. warning::
            This method is much slower than the native expressions API.
            Only use it if you cannot implement your logic otherwise.

        Using this is considered an anti-pattern as it will be very slow because:

View on GitHub (pinned to fe841f959e)