{"record":{"id":"747fe1cf9828fe73","repo":"pandas-dev/pandas","slug":"other-should-be-pandas-na-or-a-bool-got-type-o","errorCode":null,"errorMessage":"'other' should be pandas.NA or a bool. Got {type(other).__name__} instead.","messagePattern":"'other' should be pandas\\.NA or a bool\\. Got (.+?) instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/boolean.py","lineNumber":421,"sourceCode":"            ) and not ops.has_castable_attr(other):\n                warnings.warn(\n                    f\"Operation with {type(other).__name__} is deprecated. \"\n                    \"In a future version these will be treated as scalar-like. \"\n                    \"To retain the old behavior, explicitly wrap in a Series \"\n                    \"instead.\",\n                    Pandas4Warning,\n                    stacklevel=find_stack_level(),\n                )\n\n            other = np.asarray(other, dtype=\"bool\")\n            if other.ndim > 1:\n                return NotImplemented\n            other, mask = coerce_to_array(other, copy=False)\n        elif isinstance(other, np.bool_):\n            other = other.item()\n\n        if other_is_scalar and other is not libmissing.NA and not lib.is_bool(other):\n            raise TypeError(\n                \"'other' should be pandas.NA or a bool. \"\n                f\"Got {type(other).__name__} instead.\"\n            )\n\n        if not other_is_scalar and len(self) != len(other):\n            raise ValueError(\"Lengths must match\")\n\n        if op.__name__ in {\"or_\", \"ror_\"}:\n            result, mask = ops.kleene_or(self._data, other, self._mask, mask)\n        elif op.__name__ in {\"and_\", \"rand_\"}:\n            result, mask = ops.kleene_and(self._data, other, self._mask, mask)\n        else:\n            # i.e. xor, rxor\n            result, mask = ops.kleene_xor(self._data, other, self._mask, mask)\n\n        # i.e. BooleanArray\n        return self._maybe_mask_result(result, mask)\n","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/boolean.py#L403-L439","documentation":"BooleanArray._logical_method (boolean.py:421) enforces Kleene-logic semantics: when the other operand is a scalar that is neither pandas.NA nor a Python bool, it raises TypeError listing the actual type. Logical ops on BooleanArray require bool/NA operands to keep three-valued logic well-defined.","triggerScenarios":"ba & 5, ba | 'x', ba ^ 1.5, or any bitwise logical op between a BooleanArray and a non-bool scalar (int, float, str).","commonSituations":"Mixing integer flags with boolean arrays via bitwise operators; assuming NumPy-style broadcasting of arbitrary scalars into bool ops; refactor that replaced a bool with an int variable.","solutions":["Convert the operand to bool first: ba & bool(flag).","Use pandas.NA explicitly for missing logical operands.","Wrap list-like operands in a Series/array of bool instead of a scalar.","For integer-flag logic, convert the BooleanArray to Int64 and use arithmetic instead of bitwise ops."],"exampleFix":"# before\nba = pd.array([True, False], dtype=\"boolean\")\nba & 1  # raises\n\n# after\nba & True\n# or\nba & bool(1)","handlingStrategy":"type-guard","validationCode":"def safe_logical(ba, other):\n    import pandas as pd\n    import numpy as np\n    if pd.isna(other) or isinstance(other, (bool, np.bool_)):\n        return ba & other\n    return ba & bool(other)","typeGuard":"def is_bool_or_na(x) -> bool:\n    import pandas as pd\n    import numpy as np\n    return x is pd.NA or isinstance(x, (bool, np.bool_))","tryCatchPattern":"try:\n    result = ba & other\nexcept TypeError as e:\n    if \"should be pandas.NA or a bool\" in str(e):\n        result = ba & bool(other)\n    else:\n        raise","preventionTips":["Convert operands to bool before bitwise logical ops","Use pandas.NA for missing logical operands","Avoid mixing integer/float scalars into BooleanArray logical ops"],"tags":["boolean","logical-op","kleene","type-mismatch"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}