pola-rs/polars · error · TypeError

the truth value of a Series is ambiguous\n\nHere are some th

Error message

the truth value of a Series is ambiguous\n\nHere are some things you might want to try:\n- instead of `if s`, use `if not s.is_empty()`\n- instead of `s1 and s2`, use `s1 & s2`\n- instead of `s1 or s2`, use `s1 | s2`\n- instead of `s in [y, z]`, use `s.is_in([y, z])`\n

What it means

Error "the truth value of a Series is ambiguous\n\nHere are some things you might want to try:\n- instead of `if s`, use `if not s.is_empty()`\n- instead of `s1 and s2`, use `s1 & s2`\n- instead of `s1 or s2`, use `s1 | s2`\n- instead of `s in [y, z]`, use `s.is_in([y, z])`\n" thrown in pola-rs/polars.

Source

Thrown at py-polars/src/polars/series/series.py:637

        Examples
        --------
        >>> s = pl.Series("a", [1, 2, 3])
        >>> s.shape
        (3,)
        """
        return (self._s.len(),)

    def __bool__(self) -> NoReturn:
        msg = (
            "the truth value of a Series is ambiguous"
            "\n\n"
            "Here are some things you might want to try:\n"
            "- instead of `if s`, use `if not s.is_empty()`\n"
            "- instead of `s1 and s2`, use `s1 & s2`\n"
            "- instead of `s1 or s2`, use `s1 | s2`\n"
            "- instead of `s in [y, z]`, use `s.is_in([y, z])`\n"
        )
        raise TypeError(msg)

    def __getstate__(self) -> bytes:
        return self._s.__getstate__()

    def __setstate__(self, state: bytes) -> None:
        self._s = Series()._s  # Initialize with a dummy
        self._s.__setstate__(state)

    def __str__(self) -> str_:
        s_repr: str = self._s.as_str()
        return s_repr.replace("Series", f"{self.__class__.__name__}", 1)

    def __repr__(self) -> str_:
        return self.__str__()

    def __len__(self) -> int:
        return self.len()

View on GitHub (pinned to 68506541d2)

Solutions

  1. Follow the suggestions in the message: use `s.is_empty()`, `&`/`|` instead of `and`/`or`, and `s.is_in([...])` instead of `in`.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at py-polars/src/polars/series/series.py:752 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19). Data as JSON: /api/errors/4a30600e676cee4c. Report an issue: GitHub.