pandas-dev/pandas · error · NotImplementedError

operator '{op_name}' not implemented for bool dtypes

Error message

operator '{op_name}' not implemented for bool dtypes

What it means

Raised inside BaseMaskedArray._arith_method when the other operand is pandas.NA (or pd.NA-equivalent) and self.dtype is boolean, for the operators floordiv/rfloordiv/pow/rpow/truediv/rtruediv. These arithmetic combinations have no sensible result for booleans, so pandas raises NotImplementedError rather than inventing behavior (see GH#41165).

Source

Thrown at pandas/core/arrays/masked.py:1017

            #  e.g. test_array_scalar_like_equivalence
            other = bool(other)

        mask = self._propagate_mask(omask, other)

        if other is libmissing.NA:
            result = np.ones_like(self._data)
            if self.dtype.kind == "b":
                if op_name in {
                    "floordiv",
                    "rfloordiv",
                    "pow",
                    "rpow",
                    "truediv",
                    "rtruediv",
                }:
                    # GH#41165 Try to match non-masked Series behavior
                    #  This is still imperfect GH#46043
                    raise NotImplementedError(
                        f"operator '{op_name}' not implemented for bool dtypes"
                    )
                if op_name in {"mod", "rmod"}:
                    dtype = "int8"
                else:
                    dtype = "bool"
                result = result.astype(dtype)
            elif "truediv" in op_name and self.dtype.kind != "f":
                # The actual data here doesn't matter since the mask
                #  will be all-True, but since this is division, we want
                #  to end up with floating dtype.
                result = result.astype(np.float64)
            elif op_name in {"divmod", "rdivmod"}:
                # GH#62196
                res = self._maybe_mask_result(result, mask)
                return res, res.copy()
        else:
            # Make sure we do this before the "pow" mask checks

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Skip boolean columns when applying NA-propagating arithmetic; guard on dtype.kind == 'b'.
  2. Cast the BooleanArray to int8/Int8 first if integer division semantics are acceptable: bool_arr.astype('Int8') // pd.NA.
  3. Handle the boolean case explicitly (e.g. produce all-NA via np.full(len(arr), pd.NA)).

Example fix

// before
res = bool_arr // pd.NA  # raises: operator 'floordiv' not implemented for bool dtypes

// after
res = bool_arr.astype("Int8") // pd.NA
Defensive patterns

Strategy: type-guard

Validate before calling

BOOL_NA_OPS = {"floordiv", "rfloordiv", "pow", "rpow", "truediv", "rtruediv"}

def bool_dtype_safe_op(arr, op_name, other):
    if arr.dtype.kind == "b" and op_name in BOOL_NA_OPS and other is pd.NA:
        raise NotImplementedError(f"{op_name} unsupported for bool with NA")
    return getattr(arr, f"__{op_name}__")(other)

Type guard

def is_bool_na_arith(arr, op_name, other) -> bool:
    import pandas as pd
    return arr.dtype.kind == "b" and other is pd.NA and op_name in {"floordiv","rfloordiv","pow","rpow","truediv","rtruediv"}

Try / catch

try:
    res = bool_arr // pd.NA
except NotImplementedError as e:
    if "not implemented for bool dtypes" in str(e):
        res = bool_arr.astype("Int8") // pd.NA
    else:
        raise

Prevention

When it happens

Trigger: Computing bool_arr // pd.NA, bool_arr ** pd.NA, bool_arr / pd.NA (and the reflected variants) on a nullable BooleanArray.

Common situations: Generic NA-propagating code that applies the same arithmetic op to every column including boolean columns; using .floordiv(pd.NA) or division on a BooleanArray.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/0ca8eed922f37933. Report an issue: GitHub.