{"record":{"id":"9c8bd1e5b1f0bf09","repo":"pola-rs/polars","slug":"predicate-by-predicate-s-returned-no-rows","errorCode":null,"errorMessage":"predicate <{by_predicate!s}> returned no rows","messagePattern":"predicate <(.+?)> returned no rows","errorType":"exception","errorClass":"NoRowsReturnedError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":11886,"sourceCode":"        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:\n                msg = f\"predicate <{by_predicate!s}> returned {n_rows} rows\"\n                raise TooManyRowsReturnedError(msg)\n            elif n_rows == 0:\n                msg = f\"predicate <{by_predicate!s}> returned no rows\"\n                raise NoRowsReturnedError(msg)\n\n            row = rows[0]\n            if named:\n                return dict(zip(self.columns, row, strict=True))\n            else:\n                return row\n        else:\n            msg = \"one of `index` or `by_predicate` must be set\"\n            raise ValueError(msg)\n\n    @overload\n    def rows(self, *, named: Literal[False] = ...) -> list[tuple[Any, ...]]: ...\n\n    @overload\n    def rows(self, *, named: Literal[True]) -> list[dict[str, Any]]: ...\n\n    def rows(\n        self, *, named: bool = False","sourceCodeStart":11868,"sourceCodeEnd":11904,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L11868-L11904","documentation":"When DataFrame.row(by_predicate=expr) filters the frame and zero rows survive, polars raises NoRowsReturnedError (a polars.exceptions subclass) with the predicate text. This is the empty counterpart of TooManyRowsReturnedError and typically indicates the looked-up value does not exist (or the predicate never matches) rather than a programming mistake.","triggerScenarios":"df.row(by_predicate=pl.col('id') == 42) when no row has id 42; comparing with the wrong dtype or value (string vs int, naive vs timezone-aware datetimes); predicates on an empty frame; NULL-containing keys (comparisons with null never match).","commonSituations":"Key lookups for ids missing from the current snapshot; date-range lookups that miss due to timezone/precision; lookup tables loaded with filters that excluded the needed rows; empty input files in pipelines.","solutions":["Verify existence first: matched = df.filter(pred); use matched only if matched.height == 1","Check the comparison value's dtype and semantics (cast, strptime, dt.convert_time_zone) if matches are expected but absent","For optional lookups, catch polars.exceptions.NoRowsReturnedError and return a default/fallback","Handle nulls explicitly (fill_null / is_null()) since comparisons against null never match"],"exampleFix":"# before\nrow = df.row(by_predicate=pl.col('id') == 42)  # NoRowsReturnedError\n\n# after\nmatched = df.filter(pl.col('id') == 42)\nrow = matched.row(0) if matched.height == 1 else None","handlingStrategy":"try-catch","validationCode":"matched = df.filter(by_predicate)\nif matched.is_empty():\n    row = None  # or raise your own NotFound error with context\nelse:\n    row = matched.row(0)","typeGuard":null,"tryCatchPattern":"from polars.exceptions import NoRowsReturnedError\n\ntry:\n    row = df.row(by_predicate=pred)\nexcept NoRowsReturnedError:\n    row = None  # optional lookup: fall back to a default","preventionTips":["Check existence with df.filter(pred).height before one-row lookups on external/sparse data","Verify comparison dtype and semantics (cast, timezone, strptime) when matches are expected but absent","Remember null never equals anything — handle nulls with is_null()/fill_null() in predicates"],"tags":["polars","dataframe","row","no-results","predicate","lookup"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}