Skip to content

Commit 1f89b64

Browse files
authored
REF: collect IntervalArray methods by topic (#36438)
1 parent 46ad4b7 commit 1f89b64

File tree

2 files changed

+230
-208
lines changed

2 files changed

+230
-208
lines changed

pandas/core/arrays/datetimelike.py

+37-31
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
DTScalarOrNaT = Union[DatetimeLikeScalar, NaTType]
6868

6969

70+
class InvalidComparison(Exception):
71+
"""
72+
Raised by _validate_comparison_value to indicate to caller it should
73+
return invalid_comparison.
74+
"""
75+
76+
pass
77+
78+
7079
def _datetimelike_array_cmp(cls, op):
7180
"""
7281
Wrap comparison operations to convert Timestamp/Timedelta/Period-like to
@@ -75,44 +84,14 @@ def _datetimelike_array_cmp(cls, op):
7584
opname = f"__{op.__name__}__"
7685
nat_result = opname == "__ne__"
7786

78-
class InvalidComparison(Exception):
79-
pass
80-
81-
def _validate_comparison_value(self, other):
82-
if isinstance(other, str):
83-
try:
84-
# GH#18435 strings get a pass from tzawareness compat
85-
other = self._scalar_from_string(other)
86-
except ValueError:
87-
# failed to parse as Timestamp/Timedelta/Period
88-
raise InvalidComparison(other)
89-
90-
if isinstance(other, self._recognized_scalars) or other is NaT:
91-
other = self._scalar_type(other)
92-
self._check_compatible_with(other)
93-
94-
elif not is_list_like(other):
95-
raise InvalidComparison(other)
96-
97-
elif len(other) != len(self):
98-
raise ValueError("Lengths must match")
99-
100-
else:
101-
try:
102-
other = self._validate_listlike(other, opname, allow_object=True)
103-
except TypeError as err:
104-
raise InvalidComparison(other) from err
105-
106-
return other
107-
10887
@unpack_zerodim_and_defer(opname)
10988
def wrapper(self, other):
11089
if self.ndim > 1 and getattr(other, "shape", None) == self.shape:
11190
# TODO: handle 2D-like listlikes
11291
return op(self.ravel(), other.ravel()).reshape(self.shape)
11392

11493
try:
115-
other = _validate_comparison_value(self, other)
94+
other = self._validate_comparison_value(other, opname)
11695
except InvalidComparison:
11796
return invalid_comparison(self, other, op)
11897

@@ -696,6 +675,33 @@ def _from_factorized(cls, values, original):
696675
# Validation Methods
697676
# TODO: try to de-duplicate these, ensure identical behavior
698677

678+
def _validate_comparison_value(self, other, opname: str):
679+
if isinstance(other, str):
680+
try:
681+
# GH#18435 strings get a pass from tzawareness compat
682+
other = self._scalar_from_string(other)
683+
except ValueError:
684+
# failed to parse as Timestamp/Timedelta/Period
685+
raise InvalidComparison(other)
686+
687+
if isinstance(other, self._recognized_scalars) or other is NaT:
688+
other = self._scalar_type(other) # type: ignore[call-arg]
689+
self._check_compatible_with(other)
690+
691+
elif not is_list_like(other):
692+
raise InvalidComparison(other)
693+
694+
elif len(other) != len(self):
695+
raise ValueError("Lengths must match")
696+
697+
else:
698+
try:
699+
other = self._validate_listlike(other, opname, allow_object=True)
700+
except TypeError as err:
701+
raise InvalidComparison(other) from err
702+
703+
return other
704+
699705
def _validate_fill_value(self, fill_value):
700706
"""
701707
If a fill_value is passed to `take` convert it to an i8 representation,

0 commit comments

Comments
 (0)