Skip to content

Commit 9a244b5

Browse files
jbrockmendelTomAugspurger
authored andcommitted
REF: use should_extension_dispatch for comparison method (#27912)
* REF: implement should_extension_dispatch
1 parent be08902 commit 9a244b5

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed

pandas/core/ops/__init__.py

+5-31
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
from pandas.core.dtypes.common import (
1818
ensure_object,
1919
is_bool_dtype,
20-
is_categorical_dtype,
2120
is_datetime64_dtype,
22-
is_datetime64tz_dtype,
2321
is_datetimelike_v_numeric,
2422
is_extension_array_dtype,
2523
is_integer_dtype,
@@ -32,6 +30,7 @@
3230
ABCDataFrame,
3331
ABCDatetimeArray,
3432
ABCDatetimeIndex,
33+
ABCExtensionArray,
3534
ABCIndexClass,
3635
ABCSeries,
3736
ABCSparseSeries,
@@ -699,42 +698,17 @@ def wrapper(self, other, axis=None):
699698

700699
if isinstance(other, ABCSeries) and not self._indexed_same(other):
701700
raise ValueError("Can only compare identically-labeled Series objects")
702-
elif (
703-
is_list_like(other)
704-
and len(other) != len(self)
705-
and not isinstance(other, (set, frozenset))
706-
):
707-
raise ValueError("Lengths must match")
708701

709-
elif isinstance(other, (np.ndarray, ABCIndexClass, ABCSeries)):
702+
elif isinstance(
703+
other, (np.ndarray, ABCExtensionArray, ABCIndexClass, ABCSeries)
704+
):
710705
# TODO: make this treatment consistent across ops and classes.
711706
# We are not catching all listlikes here (e.g. frozenset, tuple)
712707
# The ambiguous case is object-dtype. See GH#27803
713708
if len(self) != len(other):
714709
raise ValueError("Lengths must match to compare")
715710

716-
if is_categorical_dtype(self):
717-
# Dispatch to Categorical implementation; CategoricalIndex
718-
# behavior is non-canonical GH#19513
719-
res_values = dispatch_to_extension_op(op, self, other)
720-
721-
elif is_datetime64_dtype(self) or is_datetime64tz_dtype(self):
722-
# Dispatch to DatetimeIndex to ensure identical
723-
# Series/Index behavior
724-
from pandas.core.arrays import DatetimeArray
725-
726-
res_values = dispatch_to_extension_op(op, DatetimeArray(self), other)
727-
728-
elif is_timedelta64_dtype(self):
729-
from pandas.core.arrays import TimedeltaArray
730-
731-
res_values = dispatch_to_extension_op(op, TimedeltaArray(self), other)
732-
733-
elif is_extension_array_dtype(self) or (
734-
is_extension_array_dtype(other) and not is_scalar(other)
735-
):
736-
# Note: the `not is_scalar(other)` condition rules out
737-
# e.g. other == "category"
711+
if should_extension_dispatch(self, other):
738712
res_values = dispatch_to_extension_op(op, self, other)
739713

740714
elif is_scalar(other) and isna(other):

0 commit comments

Comments
 (0)