Skip to content

Commit 29ebfa8

Browse files
committed
fixes for Timestamp/Timedelta comparison ops
1 parent dc72e04 commit 29ebfa8

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

pandas/tslib.pyx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,18 @@ cdef class _Timestamp(datetime):
758758
ots = Timestamp(other)
759759
except ValueError:
760760
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
761772
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])
768773
return NotImplemented
769774

770775
self._assert_tzawareness_compat(other)
@@ -911,7 +916,8 @@ cdef class _NaT(_Timestamp):
911916
# py3k needs this defined here
912917
return hash(self.value)
913918

914-
__array_priority__ = 0
919+
# less than np.ndarray
920+
__array_priority__ = -1
915921

916922
def __richcmp__(_NaT self, object other, int op):
917923
cdef int ndim = getattr(other, 'ndim', -1)
@@ -1519,12 +1525,13 @@ cdef class _Timedelta(timedelta):
15191525
ots = other
15201526
elif isinstance(other, timedelta):
15211527
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])
15281535
return _cmp_scalar(self.value, ots.value, op)
15291536

15301537
def _ensure_components(_Timedelta self):

0 commit comments

Comments
 (0)