{"record":{"id":"a2e811a605cf0708","repo":"pola-rs/polars","slug":"operator-r-comparison-not-supported-for-lazyfr","errorCode":null,"errorMessage":"\"{operator!r}\" comparison not supported for LazyFrame objects","messagePattern":"\"(.+?)\" comparison not supported for LazyFrame objects","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":667,"sourceCode":"        \"\"\"\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:\n        self._comparison_error(\">=\")\n\n    def __le__(self, other: Any) -> NoReturn:\n        self._comparison_error(\"<=\")","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/lazyframe/frame.py#L649-L685","documentation":"`LazyFrame` disables all comparison operators: `__eq__`, `__ne__`, `__gt__`, `__lt__`, `__ge__`, `__le__` route through `_comparison_error` (frame.py:667) and raise TypeError. A LazyFrame is a query plan, not data, so element-wise or frame-wise comparison is meaningless until execution — and allowing `==` would break hashing/identity semantics. Note `==` does NOT silently return False; it raises.","triggerScenarios":"`lf1 == lf2`, `df_col == lf` mixing, sorting/`max()` helpers that apply `>` to operands, or `lf in [other_lf]`-style membership that uses `==`; also `lf >= something` in validation code copied from eager DataFrame logic.","commonSituations":"Pandas/polars-eager habit `df1 == df2` applied to lazy frames; writing generic assertion helpers that compare two query results; using LazyFrames as dict keys or in sets (hashing invokes `==` on collision); diffing golden plans in tests via `==`.","solutions":["Collect and compare data: `lf1.collect().equals(lf2.collect())`","To compare plans in tests, serialize them: `lf1.serialize() == lf2.serialize()`","For element-wise comparison, build an expression (`pl.col('a') == pl.col('b')`) and use `select`/`with_columns` — never bare frames","Remove LazyFrames from dict/set usage; use explicit identity (`is`) or collected results"],"exampleFix":"# before\nif lf1 == lf2:  # TypeError: '==' comparison not supported\n    ...\n\n# after\nif lf1.collect().equals(lf2.collect()):\n    ...","handlingStrategy":"type-guard","validationCode":"import polars as pl\n\ndef frames_equal(a: pl.LazyFrame, b: pl.LazyFrame) -> bool:\n    'Data equality; executes both queries.'\n    return a.collect().equals(b.collect())\n\ndef plans_equal(a: pl.LazyFrame, b: pl.LazyFrame) -> bool:\n    'Plan equality without executing.'\n    return a.serialize(format='json') == b.serialize(format='json')\n\nif frames_equal(lf1, lf2):  # instead of: if lf1 == lf2:\n    ...","typeGuard":"from typing import TypeGuard\nimport polars as pl\n\ndef is_lazy_frame(x: object) -> TypeGuard[pl.LazyFrame]:\n    return isinstance(x, pl.LazyFrame)\n\n# narrow before comparison helpers that assume data (e.g. pandas frames):\nif is_lazy_frame(other):\n    equal = lf.collect().equals(other.collect())","tryCatchPattern":"try:\n    same = lf1 == lf2\nexcept TypeError as e:\n    if 'comparison not supported for LazyFrame' in str(e):\n        same = lf1.collect().equals(lf2.collect())\n    else:\n        raise","preventionTips":["Use DataFrame.equals on collected results for data equality","Use serialized plans (lf.serialize()) for golden-file tests instead of ==","Never put LazyFrames in sets or use them as dict keys; compare explicit keys instead","For element-wise logic, write column expressions (pl.col('a') == pl.col('b')) inside select/with_columns"],"tags":["polars","lazyframe","comparison","typeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}