{"record":{"id":"0f9a1e16e9f55257","repo":"pola-rs/polars","slug":"use-of-aggregate-function-count-should-be-repl","errorCode":null,"errorMessage":"use of `aggregate_function='count'` should be replaced with `aggregate_function='len'`.","messagePattern":"use of `aggregate_function='count'` should be replaced with `aggregate_function='len'`\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":8360,"sourceCode":"            elif aggregate_function == \"item\":\n                agg = agg.item()\n            elif aggregate_function == \"sum\":\n                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()","sourceCodeStart":8342,"sourceCodeEnd":8378,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/frame.py#L8342-L8378","documentation":"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'.","triggerScenarios":"Calling `lf.pivot(on=..., index=..., values=..., aggregate_function=\"count\")` — the string comparison branch raises before execution.","commonSituations":"Migrating from older polars versions where 'count' was accepted; copying pivot snippets from old blog posts or pandas-style aggregation names.","solutions":["Replace `aggregate_function=\"count\"` with `aggregate_function=\"len\"`.","Search the codebase for `aggregate_function` occurrences and audit string values.","Upgrade polars and address deprecation warnings before they become hard errors."],"exampleFix":"// before\nlf.pivot(on=\"date\", index=\"id\", values=\"sales\", aggregate_function=\"count\")\n// after\nlf.pivot(on=\"date\", index=\"id\", values=\"sales\", aggregate_function=\"len\")","handlingStrategy":"validation","validationCode":"if aggregate_function == \"count\":\n    aggregate_function = \"len\"","typeGuard":null,"tryCatchPattern":"try:\n    out = lf.pivot(on=on, index=idx, values=vals, aggregate_function=agg)\nexcept ValueError as e:\n    if \"aggregate_function='count'\" in str(e):\n        out = lf.pivot(on=on, index=idx, values=vals, aggregate_function=\"len\")\n    else:\n        raise","preventionTips":["Use 'len' instead of 'count' everywhere.","Grep for aggregate_function=\"count\" during polars upgrades.","Read polars changelogs for renamed argument values."],"tags":["python","polars","pivot","deprecated","api-change"],"backgroundTag":"deprecated-api-usage","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"}