67
67
DTScalarOrNaT = Union [DatetimeLikeScalar , NaTType ]
68
68
69
69
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
+
70
79
def _datetimelike_array_cmp (cls , op ):
71
80
"""
72
81
Wrap comparison operations to convert Timestamp/Timedelta/Period-like to
@@ -75,44 +84,14 @@ def _datetimelike_array_cmp(cls, op):
75
84
opname = f"__{ op .__name__ } __"
76
85
nat_result = opname == "__ne__"
77
86
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
-
108
87
@unpack_zerodim_and_defer (opname )
109
88
def wrapper (self , other ):
110
89
if self .ndim > 1 and getattr (other , "shape" , None ) == self .shape :
111
90
# TODO: handle 2D-like listlikes
112
91
return op (self .ravel (), other .ravel ()).reshape (self .shape )
113
92
114
93
try :
115
- other = _validate_comparison_value (self , other )
94
+ other = self . _validate_comparison_value (other , opname )
116
95
except InvalidComparison :
117
96
return invalid_comparison (self , other , op )
118
97
@@ -696,6 +675,33 @@ def _from_factorized(cls, values, original):
696
675
# Validation Methods
697
676
# TODO: try to de-duplicate these, ensure identical behavior
698
677
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
+
699
705
def _validate_fill_value (self , fill_value ):
700
706
"""
701
707
If a fill_value is passed to `take` convert it to an i8 representation,
0 commit comments