{"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":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":8234,"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":8216,"sourceCodeEnd":8252,"githubUrl":"https://github.com/pola-rs/polars/blob/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/lazyframe/frame.py#L8216-L8252","documentation":"Polars' LazyFrame.rolling/aggregation API accepts aggregate_function='len' to count rows, but 'count' was removed/deprecated because it historically was ambiguous (count of rows vs non-null values). Passing 'count' now raises a ValueError directing you to use 'len' (or 'sum' etc. for value aggregations).","triggerScenarios":"Calling an aggregation API (e.g. LazyFrame.rolling or group-by style helper in frame.py around line 8141) with the string aggregate_function='count'.","commonSituations":"Code written for older Polars versions where 'count' was accepted; copy-pasted StackOverflow answers; migrating pipelines after a Polars upgrade where 'count' was deprecated then turned into a hard error.","solutions":["Replace aggregate_function='count' with aggregate_function='len'","If you need non-null value counts, use aggregate_function='sum' on a non-null indicator or an explicit .count() expression after the aggregation","Pin/upgrade Polars deliberately and grep the codebase for aggregate_function=\"count\" during migration"],"exampleFix":"# before\nlf.rolling(index_column='ts', period='1d', aggregate_function='count')\n\n# after\nlf.rolling(index_column='ts', period='1d', aggregate_function='len')","handlingStrategy":"validation","validationCode":"allowed = {'min','max','first','last','sum','mean','median','len','std','var'}\nassert aggregate_function in allowed, f\"unsupported aggregate_function: {aggregate_function!r}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Grep codebase for aggregate_function='count' before upgrading Polars","Keep a shared constant/list of supported aggregate_function values and validate inputs against it"],"tags":["polars","lazyframe","aggregate","api-migration","deprecated-argument"],"backgroundTag":"polars-deprecated-argument-removed","analyzedSha":"68506541d2de983056c9eb244e1ea05fab377dfc","analyzedAt":"2026-08-28T18:02:35.179Z","contentChangedAt":"2026-08-28T18:02:35.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}