Skip to content

API: disallow div/floordiv/pow operators for BooleanArray ? #41165

@jorisvandenbossche

Description

@jorisvandenbossche

Currently, for the plain bool dtype we explicitly check for some operations and raise an error, while those actually work in numpy. For example:

>>> arr = np.array([True, False, True])
>>> arr / True
array([1., 0., 1.])

>>> pd.Series(arr) / True
...
NotImplementedError: operator '/' not implemented for bool dtypes

This is done for the division and power operations (not_allowed={"/", "//", "**"}):

if op_str in not_allowed:
raise NotImplementedError(
f"operator {repr(op_str)} not implemented for bool dtypes"
)

For the nullable BooleanArray, for now we simply relied on the operations as defined by the underlying numpy bool array:

>>> pd.array(arr) / True
<FloatingArray>
[1.0, 0.0, 1.0]
Length: 3, dtype: Float64

That's for the BooleanArray, but the check is currently done on the "array_op" level (but because it is done within expressions.py, we don't run that check for EAs, xref #41161).

So questions:

  • Do we actually want to continue disallowing this operation, while numpy allows it? (also, eg pd.Series(arr) / 1 does work, it's only disallowed if both operands are boolean)
  • If we keep disallowing it, we should probably also disallow it on the BooleanArray level, and not only check it on the DataFrame/Series ops level in array_ops.py ?

Metadata

Metadata

Assignees

Labels

API - ConsistencyInternal Consistency of API/BehaviorNA - MaskedArraysRelated to pd.NA and nullable extension arraysNumeric OperationsArithmetic, Comparison, and Logical operations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions