{"record":{"id":"90b326d4e8fefdc4","repo":"pola-rs/polars","slug":"cannot-set-both-index-and-by-predicate-mutual","errorCode":null,"errorMessage":"cannot set both 'index' and 'by_predicate'; mutually exclusive","messagePattern":"cannot set both 'index' and 'by_predicate'; mutually exclusive","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":11863,"sourceCode":"        {'foo': 3, 'bar': 8, 'ham': 'c'}\n\n        Use `by_predicate` to return the row that matches the given predicate.\n\n        >>> df.row(by_predicate=(pl.col(\"ham\") == \"b\"))\n        (2, 7, 'b')\n        \"\"\"\n        if index is None and by_predicate is None:\n            if self.height == 1:\n                index = 0\n            else:\n                msg = (\n                    'can only call `.row()` without \"index\" or \"by_predicate\" values '\n                    f\"if the DataFrame has a single row; shape={self.shape!r}\"\n                )\n                raise ValueError(msg)\n        elif index is not None and by_predicate is not None:\n            msg = \"cannot set both 'index' and 'by_predicate'; mutually exclusive\"\n            raise ValueError(msg)\n        elif isinstance(index, pl.Expr):\n            msg = \"expressions should be passed to the `by_predicate` parameter\"\n            raise TypeError(msg)\n\n        if index is not None:\n            row = self._df.row_tuple(index)\n            if named:\n                return dict(zip(self.columns, row, strict=True))\n            else:\n                return row\n\n        elif by_predicate is not None:\n            if not isinstance(by_predicate, pl.Expr):\n                msg = f\"expected `by_predicate` to be an expression, got {qualified_type_name(by_predicate)!r}\"\n                raise TypeError(msg)\n            rows = self.filter(by_predicate).rows()\n            n_rows = len(rows)\n            if n_rows > 1:","sourceCodeStart":11845,"sourceCodeEnd":11881,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L11845-L11881","documentation":"DataFrame.row takes mutually exclusive selectors: an integer position via `index` or a boolean expression via `by_predicate`. Passing both at once is rejected with this ValueError because the two would specify different (and possibly conflicting) rows.","triggerScenarios":"df.row(index=0, by_predicate=pl.col('id') == 42); helper functions that accept and forward both parameters unconditionally; splatting a params dict that contains both keys.","commonSituations":"Generic get-row wrappers exposing both positional and predicate selection; refactors that switched a call site to by_predicate without removing index; building call arguments dynamically from user input.","solutions":["Keep exactly one selector: use df.row(by_predicate=pl.col('id') == 42) for key-based access or df.row(0) for positional access","In wrappers, resolve precedence before calling: use the predicate when provided, else the index","When splatting dicts, ensure only one of 'index' / 'by_predicate' is present"],"exampleFix":"# before\nrow = df.row(index=0, by_predicate=pl.col('id') == 42)\n\n# after\nrow = df.row(by_predicate=pl.col('id') == 42)","handlingStrategy":"validation","validationCode":"if (index is not None) == (by_predicate is not None):\n    raise ValueError('pass exactly one of index / by_predicate')\nrow = df.row(index=index, by_predicate=by_predicate)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Choose one selection style per call site — positional or predicate, never both","In wrappers, resolve precedence (predicate wins, else index) before calling row","Keep only one of the keys present in any params dict you splat into row()"],"tags":["polars","dataframe","row","mutually-exclusive-parameters","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}