{"record":{"id":"eb5c0998b5b03b79","repo":"pola-rs/polars","slug":"the-truth-value-of-a-dataframe-is-ambiguous-hint","errorCode":null,"errorMessage":"the truth value of a DataFrame is ambiguous\n\nHint: to check if a DataFrame contains any values, use `is_empty()`.","messagePattern":"the truth value of a DataFrame is ambiguous\n\nHint: to check if a DataFrame contains any values, use `is_empty\\(\\)`\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":1205,"sourceCode":"\n    def _cast_all_from_to(\n        self, df: DataFrame, from_: frozenset[PolarsDataType], to: PolarsDataType\n    ) -> DataFrame:\n        casts = [s.cast(to).alias(s.name) for s in df if s.dtype in from_]\n        return df.with_columns(casts) if casts else df\n\n    def __floordiv__(self, other: DataFrame | Series | int | float) -> DataFrame:\n        return self._div(other, floordiv=True)\n\n    def __truediv__(self, other: DataFrame | Series | int | float) -> DataFrame:\n        return self._div(other, floordiv=False)\n\n    def __bool__(self) -> NoReturn:\n        msg = (\n            \"the truth value of a DataFrame is ambiguous\"\n            \"\\n\\nHint: to check if a DataFrame contains any values, use `is_empty()`.\"\n        )\n        raise TypeError(msg)\n\n    def __eq__(self, other: object) -> DataFrame:  # type: ignore[override]\n        return self._comp(other, \"eq\")\n\n    def __ne__(self, other: object) -> DataFrame:  # type: ignore[override]\n        return self._comp(other, \"neq\")\n\n    def __gt__(self, other: Any) -> DataFrame:\n        return self._comp(other, \"gt\")\n\n    def __lt__(self, other: Any) -> DataFrame:\n        return self._comp(other, \"lt\")\n\n    def __ge__(self, other: Any) -> DataFrame:\n        return self._comp(other, \"gt_eq\")\n\n    def __le__(self, other: Any) -> DataFrame:\n        return self._comp(other, \"lt_eq\")","sourceCodeStart":1187,"sourceCodeEnd":1223,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L1187-L1223","documentation":"Python calls DataFrame.__bool__ whenever a DataFrame is used in a boolean context (if, while, and/or/not, bool(), filter()). Truthiness for a 2-D table is undefined — empty? any true? all true? — so polars raises TypeError and the message points to is_empty() for the most common intent. This mirrors NumPy's ambiguous-truth error.","triggerScenarios":"if df: / while df: / not df; df and other or default; bool(df); using df as a predicate in filter(df); assert df (in tests); ternaries like x if df else y.","commonSituations":"Pandas habits where `if df.empty:` or truthy shortcuts were common; checking 'did the query return anything' after a filter/join; test assertions like `assert result_df`; default-value patterns df or pl.DataFrame().","solutions":["Use df.is_empty() to test for no rows","Use df.height > 0 or len(df) > 0 for 'has data'","In tests, assert on content: assert df.height == 3 or polars.testing.assert_frame_equal","For 'any/all cell true', materialize first: (df == expected).all().all_horizontal().item() or df.select(pl.all().any()).row(0)"],"exampleFix":"# before\nif df:\n    process(df)\n\n# after\nif not df.is_empty():\n    process(df)","handlingStrategy":"validation","validationCode":"if df.is_empty():\n    print('no rows')\nelif df.height > 0:\n    print(f'{df.height} rows')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Replace every `if df:` with `if not df.is_empty():` or `if df.height > 0:`","Lint for DataFrame usage in boolean contexts (mypy/pyright flag ambiguous truthiness)","In tests assert df.height or frame equality, never bare `assert df`"],"tags":["truthiness","python-semantics","dataframe","pandas-migration"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}