@@ -75,9 +75,6 @@ def _period_array_cmp(cls, op):
75
75
def wrapper (self , other ):
76
76
ordinal_op = getattr (self .asi8 , opname )
77
77
78
- if is_list_like (other ) and len (other ) != len (self ):
79
- raise ValueError ("Lengths must match" )
80
-
81
78
if isinstance (other , str ):
82
79
try :
83
80
other = self ._scalar_from_string (other )
@@ -90,18 +87,22 @@ def wrapper(self, other):
90
87
other = Period (other , freq = self .freq )
91
88
result = ordinal_op (other .ordinal )
92
89
93
- if isinstance (other , Period ):
90
+ if isinstance (other , self ._recognized_scalars ) or other is NaT :
91
+ other = self ._scalar_type (other )
94
92
self ._check_compatible_with (other )
95
93
96
- result = ordinal_op (other . ordinal )
94
+ other_i8 = self . _unbox_scalar (other )
97
95
98
- elif other is NaT :
99
- result = np . empty ( len ( self . asi8 ), dtype = bool )
100
- result .fill (nat_result )
96
+ result = op ( self . view ( "i8" ), other_i8 )
97
+ if isna ( other ):
98
+ result .fill (nat_result )
101
99
102
100
elif not is_list_like (other ):
103
101
return invalid_comparison (self , other , op )
104
102
103
+ elif len (other ) != len (self ):
104
+ raise ValueError ("Lengths must match" )
105
+
105
106
else :
106
107
if isinstance (other , list ):
107
108
# TODO: could use pd.Index to do inference?
@@ -117,7 +118,7 @@ def wrapper(self, other):
117
118
)
118
119
o_mask = isna (other )
119
120
120
- elif not is_period_dtype (other ):
121
+ elif not cls . _is_recognized_dtype (other . dtype ):
121
122
# e.g. is_timedelta64_dtype(other)
122
123
return invalid_comparison (self , other , op )
123
124
@@ -126,7 +127,7 @@ def wrapper(self, other):
126
127
127
128
self ._check_compatible_with (other )
128
129
129
- result = ordinal_op ( other .asi8 )
130
+ result = op ( self . view ( "i8" ), other .view ( "i8" ) )
130
131
o_mask = other ._isnan
131
132
132
133
if o_mask .any ():
@@ -195,6 +196,8 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, dtl.DatelikeOps):
195
196
__array_priority__ = 1000
196
197
_typ = "periodarray" # ABCPeriodArray
197
198
_scalar_type = Period
199
+ _recognized_scalars = (Period ,)
200
+ _is_recognized_dtype = is_period_dtype
198
201
199
202
# Names others delegate to us
200
203
_other_ops : List [str ] = []
0 commit comments