{"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":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":8363,"sourceCode":"                agg = agg.sum()\n            elif aggregate_function == \"max\":\n                agg = agg.max()\n            elif aggregate_function == \"min\":\n                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                msg = \"use of `aggregate_function='count'` should be replaced with `aggregate_function='len'`.\"\n                raise ValueError(msg)\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":8345,"sourceCodeEnd":8381,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/frame.py#L8345-L8381","documentation":"Raised as a ValueError when `aggregate_function` is a string that is not one of the supported names ('first', 'item', 'last', 'len'). Unknown strings are rejected up front rather than failing during execution.","triggerScenarios":"Calling `lf.pivot(..., aggregate_function=\"sum\")` or any misspelled/unsupported string like \"mean\", \"Count\", or \"lenght\".","commonSituations":"Translating pandas pivot_table aggfunc names ('sum', 'mean') directly to polars; typos in the aggregation name.","solutions":["Use one of the supported string values: 'first', 'item', 'last', or 'len'.","For other aggregations, pass an expression instead, e.g. `aggregate_function=pl.element().sum()`.","Fix typos — the message echoes the offending value."],"exampleFix":"// before\nlf.pivot(on=\"date\", index=\"id\", values=\"sales\", aggregate_function=\"sum\")\n// after\nlf.pivot(on=\"date\", index=\"id\", values=\"sales\", aggregate_function=pl.element().sum())","handlingStrategy":"validation","validationCode":"_ALLOWED = {\"first\", \"item\", \"last\", \"len\"}\nif isinstance(aggregate_function, str) and aggregate_function not in _ALLOWED:\n    raise ValueError(f\"aggregate_function must be one of {sorted(_ALLOWED)}\")","typeGuard":null,"tryCatchPattern":"try:\n    out = lf.pivot(on=on, index=idx, values=vals, aggregate_function=agg)\nexcept ValueError as e:\n    if \"invalid input for `aggregate_function`\" in str(e):\n        out = lf.pivot(on=on, index=idx, values=vals, aggregate_function=\"len\")\n    else:\n        raise","preventionTips":["Only use documented string names: first, item, last, len.","Prefer passing an expression (pl.element().sum(), etc.) for anything else.","Add a test enumerating allowed aggregate_function values."],"tags":["python","polars","pivot","invalid-argument"],"backgroundTag":"invalid-enum-value","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"}