@@ -758,13 +758,18 @@ cdef class _Timestamp(datetime):
758
758
ots = Timestamp(other)
759
759
except ValueError :
760
760
return self ._compare_outside_nanorange(other, op)
761
+ elif isinstance (other, np.datetime64):
762
+ return PyObject_RichCompareBool(self , Timestamp(other), op)
763
+ elif hasattr (other, ' dtype' ):
764
+ if self .tz is None and self .offset is None :
765
+ # allow comparison to ndarrays; use the reverse op because it's
766
+ # necessary when comparing to pd.Series
767
+ return PyObject_RichCompare(other, self .to_datetime64(),
768
+ _reverse_ops[op])
769
+ # TODO: somehow trigger normal numpy broadcasting rules even though
770
+ # we set __array_priority__ > ndarray.__array_priority__
771
+ return NotImplemented
761
772
else :
762
- if hasattr (other, ' dtype' ):
763
- self_arg = self
764
- if self .tz is None and self .offset is None :
765
- # allow comparison to ndarrays with appropriate dtype
766
- self_arg = self .to_datetime64()
767
- return PyObject_RichCompare(other, self_arg, _reverse_ops[op])
768
773
return NotImplemented
769
774
770
775
self ._assert_tzawareness_compat(other)
@@ -911,7 +916,8 @@ cdef class _NaT(_Timestamp):
911
916
# py3k needs this defined here
912
917
return hash (self .value)
913
918
914
- __array_priority__ = 0
919
+ # less than np.ndarray
920
+ __array_priority__ = - 1
915
921
916
922
def __richcmp__ (_NaT self , object other , int op ):
917
923
cdef int ndim = getattr (other, ' ndim' , - 1 )
@@ -1519,12 +1525,13 @@ cdef class _Timedelta(timedelta):
1519
1525
ots = other
1520
1526
elif isinstance (other, timedelta):
1521
1527
ots = Timedelta(other)
1522
- else :
1523
- if hasattr (other, ' dtype' ):
1524
- return PyObject_RichCompare(other, self .to_timedelta64(),
1525
- _reverse_ops[op])
1526
- return NotImplemented
1527
-
1528
+ elif isinstance (other, np.timedelta64):
1529
+ return PyObject_RichCompareBool(self , Timedelta(other), op)
1530
+ elif hasattr (other, ' dtype' ):
1531
+ # allow comparison to ndarrays; use the reverse op because it's
1532
+ # necessary when comparing to pd.Series
1533
+ return PyObject_RichCompare(other, self .to_datetime64(),
1534
+ _reverse_ops[op])
1528
1535
return _cmp_scalar(self .value, ots.value, op)
1529
1536
1530
1537
def _ensure_components (_Timedelta self ):
0 commit comments