{"record":{"id":"0ca8eed922f37933","repo":"pandas-dev/pandas","slug":"operator-op-name-not-implemented-for-bool-dtyp","errorCode":null,"errorMessage":"operator '{op_name}' not implemented for bool dtypes","messagePattern":"operator '(.+?)' not implemented for bool dtypes","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/masked.py","lineNumber":1017,"sourceCode":"            #  e.g. test_array_scalar_like_equivalence\n            other = bool(other)\n\n        mask = self._propagate_mask(omask, other)\n\n        if other is libmissing.NA:\n            result = np.ones_like(self._data)\n            if self.dtype.kind == \"b\":\n                if op_name in {\n                    \"floordiv\",\n                    \"rfloordiv\",\n                    \"pow\",\n                    \"rpow\",\n                    \"truediv\",\n                    \"rtruediv\",\n                }:\n                    # GH#41165 Try to match non-masked Series behavior\n                    #  This is still imperfect GH#46043\n                    raise NotImplementedError(\n                        f\"operator '{op_name}' not implemented for bool dtypes\"\n                    )\n                if op_name in {\"mod\", \"rmod\"}:\n                    dtype = \"int8\"\n                else:\n                    dtype = \"bool\"\n                result = result.astype(dtype)\n            elif \"truediv\" in op_name and self.dtype.kind != \"f\":\n                # The actual data here doesn't matter since the mask\n                #  will be all-True, but since this is division, we want\n                #  to end up with floating dtype.\n                result = result.astype(np.float64)\n            elif op_name in {\"divmod\", \"rdivmod\"}:\n                # GH#62196\n                res = self._maybe_mask_result(result, mask)\n                return res, res.copy()\n        else:\n            # Make sure we do this before the \"pow\" mask checks","sourceCodeStart":999,"sourceCodeEnd":1035,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/masked.py#L999-L1035","documentation":"Raised by BaseMaskedArray arithmetic when self.dtype.kind is 'b' (nullable boolean) and the operation is one of floordiv, rfloordiv, pow, rpow, truediv, rtruediv. These operators are mathematically undefined for booleans in pandas' masked-array path, so it raises NotImplementedError to match non-masked Series behavior (GH#41165).","triggerScenarios":"Computing bool_arr // 2, bool_arr ** 2, bool_arr / 2 (and the reflected variants) on a pandas 'boolean' ExtensionArray.","commonSituations":"Treating a boolean column as 0/1 integers in arithmetic; feature engineering that divides or exponentiates flag columns; porting numpy bool math (which casts to int) to pandas nullable boolean.","solutions":["Cast to an integer dtype before the op: bool_arr.astype('Int8') // 2.","Use a nullable numeric dtype for the column from the start if math is intended.","Refactor the operation to a form defined for booleans (e.g. & / | / ~)."],"exampleFix":"// before\nbool_arr = pd.array([True, False], dtype='boolean')\nbool_arr ** 2   # raises\n// after\nbool_arr.astype('Int8') ** 2","handlingStrategy":"validation","validationCode":"import pandas as pd\nbool_ops = {'floordiv','rfloordiv','pow','rpow','truediv','rtruediv'}\nif arr.dtype.kind == 'b' and op_name in bool_ops:\n    arr = arr.astype('Int8')\nresult = getattr(arr, '__' + op_name + '__')(other)","typeGuard":"def bool_op_is_defined(arr, op_name) -> bool:\n    return not (arr.dtype.kind == 'b' and op_name in {'floordiv','rfloordiv','pow','rpow','truediv','rtruediv'})","tryCatchPattern":"try:\n    result = arr // 2\nexcept NotImplementedError as e:\n    if 'not implemented for bool' in str(e):\n        result = arr.astype('Int8') // 2\n    else:\n        raise","preventionTips":["Cast boolean columns to Int8/Int16 before arithmetic ops.","Keep boolean arrays for logical ops (&, |, ~) only.","Type-treat flag columns as numeric when math is intended."],"tags":["pandas","masked-array","arithmetic","boolean"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}