Skip to content

Commit e56ddf9

Browse files
committed
REF: use unpack_zerodim_and_defer on EA methods
1 parent 0d3c5ce commit e56ddf9

File tree

4 files changed

+11
-31
lines changed

4 files changed

+11
-31
lines changed

pandas/core/arrays/base.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from pandas.core.dtypes.cast import maybe_cast_to_extension_array
2323
from pandas.core.dtypes.common import is_array_like, is_list_like
2424
from pandas.core.dtypes.dtypes import ExtensionDtype
25-
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
2625
from pandas.core.dtypes.missing import isna
2726

2827
from pandas.core import ops
@@ -1173,6 +1172,7 @@ def _create_method(cls, op, coerce_to_dtype=True):
11731172
of the underlying elements of the ExtensionArray
11741173
"""
11751174

1175+
@ops.unpack_zerodim_and_defer(op.__name__)
11761176
def _binop(self, other):
11771177
def convert_values(param):
11781178
if isinstance(param, ExtensionArray) or is_list_like(param):
@@ -1181,10 +1181,6 @@ def convert_values(param):
11811181
ovalues = [param] * len(self)
11821182
return ovalues
11831183

1184-
if isinstance(other, (ABCSeries, ABCIndexClass)):
1185-
# rely on pandas to unbox and dispatch to us
1186-
return NotImplemented
1187-
11881184
lvalues = self
11891185
rvalues = convert_values(other)
11901186

pandas/core/arrays/boolean.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
pandas_dtype,
2424
)
2525
from pandas.core.dtypes.dtypes import register_extension_dtype
26-
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
2726
from pandas.core.dtypes.missing import isna
2827

2928
from pandas.core import nanops, ops
@@ -584,13 +583,10 @@ def all(self, skipna: bool = True, **kwargs):
584583

585584
@classmethod
586585
def _create_logical_method(cls, op):
586+
@ops.unpack_zerodim_and_defer(op.__name__)
587587
def logical_method(self, other):
588-
if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):
589-
# Rely on pandas to unbox and dispatch to us.
590-
return NotImplemented
591588

592589
assert op.__name__ in {"or_", "ror_", "and_", "rand_", "xor", "rxor"}
593-
other = lib.item_from_zerodim(other)
594590
other_is_booleanarray = isinstance(other, BooleanArray)
595591
other_is_scalar = lib.is_scalar(other)
596592
mask = None
@@ -630,16 +626,14 @@ def logical_method(self, other):
630626

631627
@classmethod
632628
def _create_comparison_method(cls, op):
629+
@ops.unpack_zerodim_and_defer(op.__name__)
633630
def cmp_method(self, other):
634631
from pandas.arrays import IntegerArray
635632

636-
if isinstance(
637-
other, (ABCDataFrame, ABCSeries, ABCIndexClass, IntegerArray)
638-
):
633+
if isinstance(other, IntegerArray):
639634
# Rely on pandas to unbox and dispatch to us.
640635
return NotImplemented
641636

642-
other = lib.item_from_zerodim(other)
643637
mask = None
644638

645639
if isinstance(other, BooleanArray):
@@ -735,13 +729,8 @@ def _maybe_mask_result(self, result, mask, other, op_name: str):
735729
def _create_arithmetic_method(cls, op):
736730
op_name = op.__name__
737731

732+
@ops.unpack_zerodim_and_defer(op_name)
738733
def boolean_arithmetic_method(self, other):
739-
740-
if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):
741-
# Rely on pandas to unbox and dispatch to us.
742-
return NotImplemented
743-
744-
other = lib.item_from_zerodim(other)
745734
mask = None
746735

747736
if isinstance(other, BooleanArray):

pandas/core/arrays/numpy_.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
from pandas.util._validators import validate_fillna_kwargs
1212

1313
from pandas.core.dtypes.dtypes import ExtensionDtype
14-
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
1514
from pandas.core.dtypes.inference import is_array_like
1615
from pandas.core.dtypes.missing import isna
1716

1817
from pandas import compat
19-
from pandas.core import nanops
18+
from pandas.core import nanops, ops
2019
from pandas.core.algorithms import searchsorted
2120
from pandas.core.array_algos import masked_reductions
2221
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
@@ -437,11 +436,9 @@ def __invert__(self):
437436

438437
@classmethod
439438
def _create_arithmetic_method(cls, op):
439+
@ops.unpack_zerodim_and_defer(op.__name__)
440440
def arithmetic_method(self, other):
441-
if isinstance(other, (ABCIndexClass, ABCSeries)):
442-
return NotImplemented
443-
444-
elif isinstance(other, cls):
441+
if isinstance(other, cls):
445442
other = other._ndarray
446443

447444
with np.errstate(all="ignore"):

pandas/core/arrays/string_.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pandas.core.dtypes.base import ExtensionDtype
99
from pandas.core.dtypes.common import pandas_dtype
1010
from pandas.core.dtypes.dtypes import register_extension_dtype
11-
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
1211
from pandas.core.dtypes.inference import is_array_like
1312

1413
from pandas import compat
@@ -302,15 +301,14 @@ def memory_usage(self, deep=False):
302301
@classmethod
303302
def _create_arithmetic_method(cls, op):
304303
# Note: this handles both arithmetic and comparison methods.
304+
305+
@ops.unpack_zerodim_and_defer(op.__name__)
305306
def method(self, other):
306307
from pandas.arrays import BooleanArray
307308

308309
assert op.__name__ in ops.ARITHMETIC_BINOPS | ops.COMPARISON_BINOPS
309310

310-
if isinstance(other, (ABCIndexClass, ABCSeries, ABCDataFrame)):
311-
return NotImplemented
312-
313-
elif isinstance(other, cls):
311+
if isinstance(other, cls):
314312
other = other._ndarray
315313

316314
mask = isna(self) | isna(other)

0 commit comments

Comments
 (0)