pola-rs/polars · error · TypeError

the truth value of a LazyFrame is ambiguous LazyFrames…

Error message

the truth value of a LazyFrame is ambiguous

LazyFrames cannot be used in boolean context with and/or/not operators.

What it means

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.

Solutions

  1. Use & and | with parentheses for combining filter expressions: lf.filter((lf.x > 1) & (lf.y < 2)).
  2. Call .collect() and evaluate the resulting DataFrame/series if you need a truth value (e.g. `if not lf.collect().is_empty():`).
  3. Replace `assert lf` with a real check on collected data, such as assert lf.collect().height > 0.

Example fix

// before
if lf:
    lf = lf.filter(lf.x > 1 and lf.y < 2)

// after
if not lf.collect().is_empty():
    lf = lf.filter((lf.x > 1) & (lf.y < 2))
Defensive patterns

Strategy: try-catch

Try / catch

try:
    result = lf1 and lf2
except TypeError as e:
    if 'truth value of a LazyFrame is ambiguous' in str(e):
        result = lf1.collect().is_empty() and lf2.collect().is_empty()
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Newcomers translating pandas/in-memory idioms; filtering with Python logical operators instead of & / |; forgetting .collect() before testing data.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/a747bba2d1c9b797. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/lazyframe/frame.py:723

        ...     }
        ... )
        >>> lf.width  # doctest: +SKIP
        2
        """
        issue_warning(
            "determining the width of a LazyFrame requires resolving its schema,"
            " which is a potentially expensive operation. Use `LazyFrame.collect_schema().len()`"
            " to get the width without this warning.",
            category=PerformanceWarning,
        )
        return self.collect_schema().len()

    def __bool__(self) -> NoReturn:
        msg = (
            "the truth value of a LazyFrame is ambiguous"
            "\n\nLazyFrames cannot be used in boolean context with and/or/not operators."
        )
        raise TypeError(msg)

    def _comparison_error(self, operator: str) -> NoReturn:
        msg = f'"{operator!r}" comparison not supported for LazyFrame objects'
        raise TypeError(msg)

    def __eq__(self, other: object) -> NoReturn:
        self._comparison_error("==")

    def __ne__(self, other: object) -> NoReturn:
        self._comparison_error("!=")

    def __gt__(self, other: Any) -> NoReturn:
        self._comparison_error(">")

    def __lt__(self, other: Any) -> NoReturn:
        self._comparison_error("<")

    def __ge__(self, other: Any) -> NoReturn:

View on GitHub (pinned to fe841f959e)