|
23 | 23 | from pandas.core.dtypes.missing import isna
|
24 | 24 |
|
25 | 25 | from pandas.core import ops
|
| 26 | +from pandas.core.arraylike import OpsMixin |
26 | 27 |
|
27 | 28 | from .masked import BaseMaskedArray, BaseMaskedDtype
|
28 | 29 |
|
@@ -202,7 +203,7 @@ def coerce_to_array(
|
202 | 203 | return values, mask
|
203 | 204 |
|
204 | 205 |
|
205 |
| -class BooleanArray(BaseMaskedArray): |
| 206 | +class BooleanArray(OpsMixin, BaseMaskedArray): |
206 | 207 | """
|
207 | 208 | Array of boolean (True/False) data with missing values.
|
208 | 209 |
|
@@ -603,52 +604,44 @@ def logical_method(self, other):
|
603 | 604 | name = f"__{op.__name__}__"
|
604 | 605 | return set_function_name(logical_method, name, cls)
|
605 | 606 |
|
606 |
| - @classmethod |
607 |
| - def _create_comparison_method(cls, op): |
608 |
| - @ops.unpack_zerodim_and_defer(op.__name__) |
609 |
| - def cmp_method(self, other): |
610 |
| - from pandas.arrays import FloatingArray, IntegerArray |
| 607 | + def _cmp_method(self, other, op): |
| 608 | + from pandas.arrays import FloatingArray, IntegerArray |
611 | 609 |
|
612 |
| - if isinstance(other, (IntegerArray, FloatingArray)): |
613 |
| - return NotImplemented |
| 610 | + if isinstance(other, (IntegerArray, FloatingArray)): |
| 611 | + return NotImplemented |
614 | 612 |
|
615 |
| - mask = None |
| 613 | + mask = None |
616 | 614 |
|
617 |
| - if isinstance(other, BooleanArray): |
618 |
| - other, mask = other._data, other._mask |
| 615 | + if isinstance(other, BooleanArray): |
| 616 | + other, mask = other._data, other._mask |
619 | 617 |
|
620 |
| - elif is_list_like(other): |
621 |
| - other = np.asarray(other) |
622 |
| - if other.ndim > 1: |
623 |
| - raise NotImplementedError( |
624 |
| - "can only perform ops with 1-d structures" |
625 |
| - ) |
626 |
| - if len(self) != len(other): |
627 |
| - raise ValueError("Lengths must match to compare") |
| 618 | + elif is_list_like(other): |
| 619 | + other = np.asarray(other) |
| 620 | + if other.ndim > 1: |
| 621 | + raise NotImplementedError("can only perform ops with 1-d structures") |
| 622 | + if len(self) != len(other): |
| 623 | + raise ValueError("Lengths must match to compare") |
628 | 624 |
|
629 |
| - if other is libmissing.NA: |
630 |
| - # numpy does not handle pd.NA well as "other" scalar (it returns |
631 |
| - # a scalar False instead of an array) |
632 |
| - result = np.zeros_like(self._data) |
633 |
| - mask = np.ones_like(self._data) |
634 |
| - else: |
635 |
| - # numpy will show a DeprecationWarning on invalid elementwise |
636 |
| - # comparisons, this will raise in the future |
637 |
| - with warnings.catch_warnings(): |
638 |
| - warnings.filterwarnings("ignore", "elementwise", FutureWarning) |
639 |
| - with np.errstate(all="ignore"): |
640 |
| - result = op(self._data, other) |
641 |
| - |
642 |
| - # nans propagate |
643 |
| - if mask is None: |
644 |
| - mask = self._mask.copy() |
645 |
| - else: |
646 |
| - mask = self._mask | mask |
| 625 | + if other is libmissing.NA: |
| 626 | + # numpy does not handle pd.NA well as "other" scalar (it returns |
| 627 | + # a scalar False instead of an array) |
| 628 | + result = np.zeros_like(self._data) |
| 629 | + mask = np.ones_like(self._data) |
| 630 | + else: |
| 631 | + # numpy will show a DeprecationWarning on invalid elementwise |
| 632 | + # comparisons, this will raise in the future |
| 633 | + with warnings.catch_warnings(): |
| 634 | + warnings.filterwarnings("ignore", "elementwise", FutureWarning) |
| 635 | + with np.errstate(all="ignore"): |
| 636 | + result = op(self._data, other) |
647 | 637 |
|
648 |
| - return BooleanArray(result, mask, copy=False) |
| 638 | + # nans propagate |
| 639 | + if mask is None: |
| 640 | + mask = self._mask.copy() |
| 641 | + else: |
| 642 | + mask = self._mask | mask |
649 | 643 |
|
650 |
| - name = f"__{op.__name__}" |
651 |
| - return set_function_name(cmp_method, name, cls) |
| 644 | + return BooleanArray(result, mask, copy=False) |
652 | 645 |
|
653 | 646 | def _reduce(self, name: str, skipna: bool = True, **kwargs):
|
654 | 647 |
|
@@ -741,5 +734,4 @@ def boolean_arithmetic_method(self, other):
|
741 | 734 |
|
742 | 735 |
|
743 | 736 | BooleanArray._add_logical_ops()
|
744 |
| -BooleanArray._add_comparison_ops() |
745 | 737 | BooleanArray._add_arithmetic_ops()
|
0 commit comments