{"record":{"id":"6d59ff093ac8f31f","repo":"pola-rs/polars","slug":"invalid-input-for-aggregate-function-argument","errorCode":null,"errorMessage":"invalid input for `aggregate_function` argument: {aggregate_function!r}","messagePattern":"invalid input for `aggregate_function` argument: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":8682,"sourceCode":"                agg = agg.min()\n            elif aggregate_function == \"mean\":\n                agg = agg.mean()\n            elif aggregate_function == \"median\":\n                agg = agg.median()\n            elif aggregate_function == \"last\":\n                agg = agg.last()\n            elif aggregate_function == \"len\":\n                agg = agg.len()\n            elif aggregate_function == \"count\":\n                issue_deprecation_warning(\n                    \"`aggregate_function='count'` input for `pivot` is deprecated.\"\n                    \" Please use `aggregate_function='len'`.\",\n                    version=\"0.20.5\",\n                )\n                agg = agg.len()\n            else:\n                msg = f\"invalid input for `aggregate_function` argument: {aggregate_function!r}\"\n                raise ValueError(msg)\n        elif aggregate_function is None:\n            agg = agg.item(allow_empty=True)\n        else:\n            agg = aggregate_function\n\n        on_cols: pl.DataFrame\n        if isinstance(on_columns, pl.DataFrame):\n            on_cols = on_columns\n        elif isinstance(on_columns, pl.Series):\n            on_cols = on_columns.to_frame()\n        elif isinstance(on_columns, str):\n            msg = f\"invalid type for `on_columns` argument: {qualified_type_name(on_columns)!r}\"\n            raise TypeError(msg)\n        else:\n            on_cols = pl.Series(values=on_columns).to_frame()\n\n        return self._from_pyldf(\n            self._ldf.pivot(","sourceCodeStart":8664,"sourceCodeEnd":8700,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/lazyframe/frame.py#L8664-L8700","documentation":"LazyFrame.pivot()'s aggregate_function, when passed as a string, must be one of 'first', 'last', 'item', 'sum', 'len', 'min', 'max' (plus deprecated 'count' which maps to 'len'). Any other string raises ValueError. Alternatively pass None (requires unique combinations) or an actual pl.Expr for custom aggregation.","triggerScenarios":"lf.pivot(..., aggregate_function='mean') — 'mean' is not in the allowed set; passing 'avg', 'median', or a function name from another library (e.g. numpy or pandas agg strings like 'size'); typos like 'Sum'.","commonSituations":"Developers assuming pandas .agg() string vocabulary carries over; dynamic aggregation selection from user config; older code using 'count' (deprecated but still works with a warning).","solutions":["Use one of the allowed strings: 'first', 'last', 'item', 'sum', 'min', 'max', 'len'","For 'mean'-style aggregation not in the list, pass a pl.Expr: aggregate_function=pl.element().mean()","Replace 'count' with 'len' (count is deprecated)","Validate config values against the allowed set before calling pivot"],"exampleFix":"# before\nwide = lf.pivot(on='month', index='id', aggregate_function='mean')\n\n# after\nimport polars as pl\nwide = lf.pivot(on='month', index='id', aggregate_function=pl.element().mean())","handlingStrategy":"validation","validationCode":"ALLOWED = {'first', 'last', 'item', 'sum', 'min', 'max', 'len'}\nif isinstance(aggregate_function, str) and aggregate_function not in ALLOWED:\n    raise ValueError(f'unsupported aggregate_function: {aggregate_function!r}')\nlf.pivot(on=on, index=index, aggregate_function=aggregate_function)","typeGuard":"def is_supported_pivot_agg(agg) -> bool:\n    import polars as pl\n    allowed = {'first', 'last', 'item', 'sum', 'min', 'max', 'len'}\n    return agg is None or (isinstance(agg, str) and agg in allowed) or isinstance(agg, pl.Expr)","tryCatchPattern":null,"preventionTips":["Don't reuse pandas agg vocabulary; polars' set is smaller","Pass a pl.Expr (e.g. pl.element().mean()) for aggregations outside the string set","Validate user-supplied aggregation names against an allowlist at config load"],"tags":["polars","lazyframe","pivot","aggregate-function","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}