{"record":{"id":"99e00348f433c4d8","repo":"pola-rs/polars","slug":"specifying-aggregations-as-a-dictionary-is-not-sup","errorCode":null,"errorMessage":"specifying aggregations as a dictionary is not supported\n\nTry unpacking the dictionary to take advantage of the keyword syntax of the `agg` method.","messagePattern":"specifying aggregations as a dictionary is not supported\n\nTry unpacking the dictionary to take advantage of the keyword syntax of the `agg` method\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/group_by.py","lineNumber":187,"sourceCode":"        ... ).collect()  # doctest: +IGNORE_RESULT\n        shape: (3, 3)\n        ┌─────┬───────┬────────────────┐\n        │ a   ┆ b_sum ┆ c_mean_squared │\n        │ --- ┆ ---   ┆ ---            │\n        │ str ┆ i64   ┆ f64            │\n        ╞═════╪═══════╪════════════════╡\n        │ a   ┆ 2     ┆ 17.0           │\n        │ c   ┆ 3     ┆ 1.0            │\n        │ b   ┆ 5     ┆ 10.0           │\n        └─────┴───────┴────────────────┘\n        \"\"\"\n        if aggs and isinstance(aggs[0], dict):\n            msg = (\n                \"specifying aggregations as a dictionary is not supported\"\n                \"\\n\\nTry unpacking the dictionary to take advantage of the keyword syntax\"\n                \" of the `agg` method.\"\n            )\n            raise TypeError(msg)\n\n        pyexprs = parse_into_list_of_expressions(*aggs, **named_aggs)\n        return wrap_ldf(self.lgb.agg(pyexprs))\n\n    def map_groups(\n        self,\n        function: Callable[[DataFrame], DataFrame],\n        schema: SchemaDict | None,\n    ) -> LazyFrame:\n        \"\"\"\n        Apply a custom/user-defined function (UDF) over the groups as a new DataFrame.\n\n        .. warning::\n            This method is much slower than the native expressions API.\n            Only use it if you cannot implement your logic otherwise.\n\n        Using this is considered an anti-pattern as it will be very slow because:\n","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/group_by.py#L169-L205","documentation":"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.","triggerScenarios":"Calling df.group_by(...).agg({\"a\": \"sum\", \"b\": \"mean\"}) or passing a dict variable as the first positional argument to agg.","commonSituations":"Migrating pandas code or older polars (<0.10) code that used the dict syntax; dynamically building aggregations into a dict.","solutions":["Unpack the dict into keyword/expression syntax: agg(pl.col(\"a\").sum(), pl.col(\"b\").mean())","Use keyword args for named outputs: agg(total=pl.col(\"a\").sum())","Build a list of expressions programmatically and unpack: agg(*exprs)"],"exampleFix":"// before\ndf.group_by(\"g\").agg({\"a\": \"sum\", \"b\": \"mean\"})\n// after\ndf.group_by(\"g\").agg(pl.col(\"a\").sum(), pl.col(\"b\").mean())","handlingStrategy":"type-guard","validationCode":"if isinstance(aggs, dict):\n    aggs = [pl.col(k).__getattribute__(v)() if isinstance(v, str) else v.alias(k) for k, v in aggs.items()]\ndf.group_by(\"g\").agg(*aggs)","typeGuard":"def is_dict_agg(x): return isinstance(x, dict)\n# convert before calling agg if True","tryCatchPattern":"try:\n    lf.agg(aggs)\nexcept TypeError:\n    lf.agg(**aggs)  # if values are expression-compatible","preventionTips":["Always pass expressions, never dicts, to GroupBy.agg","When migrating from pandas, rewrite agg_many syntax to pl.col expressions","Build dynamic aggregations as a list of pl expressions and unpack with *"],"tags":["python","polars","groupby","api-migration"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}