{"record":{"id":"a747bba2d1c9b797","repo":"pola-rs/polars","slug":"the-truth-value-of-a-lazyframe-is-ambiguous-lazyf","errorCode":null,"errorMessage":"the truth value of a LazyFrame is ambiguous\n\nLazyFrames cannot be used in boolean context with and/or/not operators.","messagePattern":"the truth value of a LazyFrame is ambiguous\n\nLazyFrames cannot be used in boolean context with and/or/not operators\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":723,"sourceCode":"        ...     }\n        ... )\n        >>> lf.width  # doctest: +SKIP\n        2\n        \"\"\"\n        issue_warning(\n            \"determining the width of a LazyFrame requires resolving its schema,\"\n            \" which is a potentially expensive operation. Use `LazyFrame.collect_schema().len()`\"\n            \" to get the width without this warning.\",\n            category=PerformanceWarning,\n        )\n        return self.collect_schema().len()\n\n    def __bool__(self) -> NoReturn:\n        msg = (\n            \"the truth value of a LazyFrame is ambiguous\"\n            \"\\n\\nLazyFrames cannot be used in boolean context with and/or/not operators.\"\n        )\n        raise TypeError(msg)\n\n    def _comparison_error(self, operator: str) -> NoReturn:\n        msg = f'\"{operator!r}\" comparison not supported for LazyFrame objects'\n        raise TypeError(msg)\n\n    def __eq__(self, other: object) -> NoReturn:\n        self._comparison_error(\"==\")\n\n    def __ne__(self, other: object) -> NoReturn:\n        self._comparison_error(\"!=\")\n\n    def __gt__(self, other: Any) -> NoReturn:\n        self._comparison_error(\">\")\n\n    def __lt__(self, other: Any) -> NoReturn:\n        self._comparison_error(\"<\")\n\n    def __ge__(self, other: Any) -> NoReturn:","sourceCodeStart":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/frame.py#L705-L741","documentation":"LazyFrame defines __bool__ to always raise TypeError because a LazyFrame has no meaningful truth value. Using it with if, and, or, not, or implicit boolean coercion is almost always a mistake — the user probably meant to evaluate collected data or combine expressions.","triggerScenarios":"Writing `if lf:` , `lf1 and lf2`, `not lf`, or `assert lf` on a LazyFrame; also filtering with Python booleans like `lf.filter((lf.x > 1) and (lf.y < 2))` where and/or short-circuit on the frame expressions.","commonSituations":"Newcomers translating pandas/in-memory idioms; filtering with Python logical operators instead of & / |; forgetting .collect() before testing data.","solutions":["Use & and | with parentheses for combining filter expressions: lf.filter((lf.x > 1) & (lf.y < 2)).","Call .collect() and evaluate the resulting DataFrame/series if you need a truth value (e.g. `if not lf.collect().is_empty():`).","Replace `assert lf` with a real check on collected data, such as assert lf.collect().height > 0."],"exampleFix":"// before\nif lf:\n    lf = lf.filter(lf.x > 1 and lf.y < 2)\n\n// after\nif not lf.collect().is_empty():\n    lf = lf.filter((lf.x > 1) & (lf.y < 2))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    result = lf1 and lf2\nexcept TypeError as e:\n    if 'truth value of a LazyFrame is ambiguous' in str(e):\n        result = lf1.collect().is_empty() and lf2.collect().is_empty()\n    else:\n        raise","preventionTips":["Use & and | (with parentheses) for combining Polars filter expressions, never `and`/`or`.","Call .collect() before any boolean test on a LazyFrame.","Enable a linter rule or code review habit flagging bare LazyFrame usage in boolean context."],"tags":["polars","boolean-context","typeerror","lazyframe"],"backgroundTag":"unsupported-operation","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"}