-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Open
Labels
API - ConsistencyInternal Consistency of API/BehaviorInternal Consistency of API/BehaviorNA - MaskedArraysRelated to pd.NA and nullable extension arraysRelated to pd.NA and nullable extension arraysNumeric OperationsArithmetic, Comparison, and Logical operationsArithmetic, Comparison, and Logical operations
Description
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={"/", "//", "**"}
):
pandas/pandas/core/computation/expressions.py
Lines 215 to 218 in 934cad6
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 inarray_ops.py
?
Metadata
Metadata
Assignees
Labels
API - ConsistencyInternal Consistency of API/BehaviorInternal Consistency of API/BehaviorNA - MaskedArraysRelated to pd.NA and nullable extension arraysRelated to pd.NA and nullable extension arraysNumeric OperationsArithmetic, Comparison, and Logical operationsArithmetic, Comparison, and Logical operations