Skip to content

Commit 2fd29dc

Browse files
jbrockmendelproost
authored andcommitted
REF: prepare Series comparison op to be refactored to array op (pandas-dev#28396)
1 parent 9a2f276 commit 2fd29dc

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

pandas/core/ops/__init__.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -663,27 +663,9 @@ def _comp_method_SERIES(cls, op, special):
663663
"""
664664
op_name = _get_op_name(op, special)
665665

666-
def na_op(x, y):
667-
# TODO:
668-
# should have guarantees on what x, y can be type-wise
669-
# Extension Dtypes are not called here
670-
671-
if is_object_dtype(x.dtype):
672-
result = comp_method_OBJECT_ARRAY(op, x, y)
673-
674-
else:
675-
method = getattr(x, op_name)
676-
with np.errstate(all="ignore"):
677-
result = method(y)
678-
if result is NotImplemented:
679-
return invalid_comparison(x, y, op)
680-
681-
return result
682-
683666
def wrapper(self, other):
684667

685668
res_name = get_op_result_name(self, other)
686-
other = lib.item_from_zerodim(other)
687669

688670
# TODO: shouldn't we be applying finalize whenever
689671
# not isinstance(other, ABCSeries)?
@@ -693,20 +675,19 @@ def wrapper(self, other):
693675
else x
694676
)
695677

696-
if isinstance(other, list):
697-
# TODO: same for tuples?
698-
other = np.asarray(other)
699-
700678
if isinstance(other, ABCDataFrame): # pragma: no cover
701679
# Defer to DataFrame implementation; fail early
702680
return NotImplemented
703681

704682
if isinstance(other, ABCSeries) and not self._indexed_same(other):
705683
raise ValueError("Can only compare identically-labeled Series objects")
706684

707-
elif isinstance(
708-
other, (np.ndarray, ABCExtensionArray, ABCIndexClass, ABCSeries)
709-
):
685+
other = lib.item_from_zerodim(other)
686+
if isinstance(other, list):
687+
# TODO: same for tuples?
688+
other = np.asarray(other)
689+
690+
if isinstance(other, (np.ndarray, ABCExtensionArray, ABCIndexClass)):
710691
# TODO: make this treatment consistent across ops and classes.
711692
# We are not catching all listlikes here (e.g. frozenset, tuple)
712693
# The ambiguous case is object-dtype. See GH#27803
@@ -726,9 +707,17 @@ def wrapper(self, other):
726707
else:
727708
res_values = np.zeros(len(lvalues), dtype=bool)
728709

710+
elif is_object_dtype(lvalues.dtype):
711+
res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
712+
729713
else:
714+
op_name = "__{op}__".format(op=op.__name__)
715+
method = getattr(lvalues, op_name)
730716
with np.errstate(all="ignore"):
731-
res_values = na_op(lvalues, rvalues)
717+
res_values = method(rvalues)
718+
719+
if res_values is NotImplemented:
720+
res_values = invalid_comparison(lvalues, rvalues, op)
732721
if is_scalar(res_values):
733722
raise TypeError(
734723
"Could not compare {typ} type with Series".format(typ=type(rvalues))

0 commit comments

Comments
 (0)