{"record":{"id":"f798018947f82f83","repo":"pola-rs/polars","slug":"pivot-needs-either-index-or-values-needs-to-b","errorCode":null,"errorMessage":"`pivot` needs either `index or `values` needs to be specified","messagePattern":"`pivot` needs either `index or `values` needs to be specified","errorType":"validation","errorClass":"InvalidOperationError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":8336,"sourceCode":"        ╞══════╪══════════╪══════════╡\n        │ a    ┆ 0.998347 ┆ null     │\n        │ b    ┆ 0.964028 ┆ 0.999954 │\n        └──────┴──────────┴──────────┘\n        \"\"\"  # noqa: W505\n        on_selector = parse_list_into_selector(on)\n\n        if index is not None and values is not None:\n            index_selector = parse_list_into_selector(index)\n            values_selector = parse_list_into_selector(values)\n        elif index is not None:\n            index_selector = parse_list_into_selector(index)\n            values_selector = cs.all() - on_selector - index_selector\n        elif values is not None:\n            values_selector = parse_list_into_selector(values)\n            index_selector = cs.all() - on_selector - values_selector\n        else:\n            msg = \"`pivot` needs either `index or `values` needs to be specified\"\n            raise InvalidOperationError(msg)\n\n        agg = F.element()\n        if isinstance(aggregate_function, str):\n            if aggregate_function == \"first\":\n                agg = agg.first()\n            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\":","sourceCodeStart":8318,"sourceCodeEnd":8354,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/frame.py#L8318-L8354","documentation":"This InvalidOperationError is raised by LazyFrame.pivot when neither `index` nor `values` is provided. The pivot needs at least one of these to know which columns stay as rows and which are aggregated; without either there is no well-defined output schema.","triggerScenarios":"Calling `LazyFrame.pivot(on=...)` with both `index=None` and `values=None`, so the code falls into the final `else` branch of the index/values resolution.","commonSituations":"Refactoring a DataFrame.pivot call to LazyFrame.pivot and dropping the `values` argument; dynamically building pivot kwargs from a config dict where both keys are absent.","solutions":["Pass `values=[...]` naming the columns to aggregate.","Pass `index=[...]` naming the columns to keep as row identifiers (values then defaults to all remaining columns).","Pass both `index` and `values` for full control of the pivot shape."],"exampleFix":"// before\nlf.pivot(on=\"date\")\n// after\nlf.pivot(on=\"date\", index=\"id\", values=\"sales\")","handlingStrategy":"validation","validationCode":"if index is None and values is None:\n    raise ValueError(\"pivot requires at least one of `index` or `values`\")\nout = lf.pivot(on=on, index=index, values=values)","typeGuard":null,"tryCatchPattern":"from polars.exceptions import InvalidOperationError\ntry:\n    out = lf.pivot(on=on, index=index, values=values)\nexcept InvalidOperationError:\n    out = lf.pivot(on=on, index=index or [\"id\"], values=values)","preventionTips":["Always pass both index and values explicitly in production code.","Centralize pivot construction in a helper that validates kwargs.","Add unit tests covering dynamic pivot argument building."],"tags":["python","polars","lazyframe","pivot","invalid-operation"],"backgroundTag":"missing-required-argument","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"}