pandas-dev/pandas · error · TypeError

'other' should be pandas.NA or a bool. Got

Error message

'other' should be pandas.NA or a bool. Got {type(other).__name__} instead.

What it means

Raised by BooleanArray._logical_method when the 'other' operand is a scalar that is neither pandas.NA nor a Python bool. Boolean logical ops (and/or/xor with Kleene logic) only accept NA, True, False, or array-likes of bool; any other scalar type (int, str, float) is rejected to avoid silent truthiness coercion.

Solutions

  1. Convert the scalar to a bool (or pd.NA) before the operation: bool(other).
  2. If you have 0/1 integers, wrap as a BooleanArray-aligned operand: pd.array([1,0,1], dtype='boolean').
  3. Use .astype('boolean') on an integer Series before combining with a BooleanArray Series.

Example fix

// before
s = pd.array([True, False, None], dtype='boolean')
out = s & 1
// after
out = s & True
Defensive patterns

Strategy: type-guard

Validate before calling

from pandas._libs import lib
from pandas import NA
if lib.is_scalar(other) and other is not NA and not lib.is_bool(other):
    raise TypeError(f'other must be NA or bool, got {type(other).__name__}')

Type guard

def is_bool_or_na(x) -> bool:
    from pandas._libs import lib
    from pandas import NA
    return x is NA or (lib.is_scalar(x) and lib.is_bool(x))

Try / catch

try:
    result = boolean_array & other
except TypeError as e:
    if "should be pandas.NA or a bool" in str(e):
        other = bool(other)
        ...

Prevention

When it happens

Trigger: boolean_series & 1; boolean_series | 'yes'; boolean_array ^ 0.5. Reached through &, |, ^ operators and their reflected variants on a BooleanArray-backed Series.

Common situations: Using 0/1 integers instead of True/False; passing strings that look boolean; mixing nullable boolean Series with non-bool scalars in vectorized expressions.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/747fe1cf9828fe73. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/boolean.py:421

            ) and not ops.has_castable_attr(other):
                warnings.warn(
                    f"Operation with {type(other).__name__} is deprecated. "
                    "In a future version these will be treated as scalar-like. "
                    "To retain the old behavior, explicitly wrap in a Series "
                    "instead.",
                    Pandas4Warning,
                    stacklevel=find_stack_level(),
                )

            other = np.asarray(other, dtype="bool")
            if other.ndim > 1:
                return NotImplemented
            other, mask = coerce_to_array(other, copy=False)
        elif isinstance(other, np.bool_):
            other = other.item()

        if other_is_scalar and other is not libmissing.NA and not lib.is_bool(other):
            raise TypeError(
                "'other' should be pandas.NA or a bool. "
                f"Got {type(other).__name__} instead."
            )

        if not other_is_scalar and len(self) != len(other):
            raise ValueError("Lengths must match")

        if op.__name__ in {"or_", "ror_"}:
            result, mask = ops.kleene_or(self._data, other, self._mask, mask)
        elif op.__name__ in {"and_", "rand_"}:
            result, mask = ops.kleene_and(self._data, other, self._mask, mask)
        else:
            # i.e. xor, rxor
            result, mask = ops.kleene_xor(self._data, other, self._mask, mask)

        # i.e. BooleanArray
        return self._maybe_mask_result(result, mask)

View on GitHub (pinned to 3b7651241d)