Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Datetimelike
Timedelta
^^^^^^^^^

-
- Bug with comparisons between :class:`Timedelta` and ``NaT`` raising ``TypeError`` (:issue:`26039`)
-
-

Expand Down
18 changes: 11 additions & 7 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,17 @@ cdef class _Timedelta(timedelta):
return PyObject_RichCompare(np.array([self]), other, op)
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
raise TypeError('Cannot compare type {cls} with type {other}'
.format(cls=type(self).__name__,
other=type(other).__name__))
if other is NaT:
return PyObject_RichCompare(other, self, reverse_ops[op])
else:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
raise TypeError('Cannot compare type {cls} with '
'type {other}'
.format(cls=type(self).__name__,
other=type(other).__name__))

return cmp_scalar(self.value, ots.value, op)

Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,34 @@ def test_timedelta(self, freq):
tm.assert_index_equal(result1, result4)
tm.assert_index_equal(result2, result3)

def test_timedelta_nat_comparisons(self):
# GH 26039
td = pd.Timedelta(0)

result = td > NaT
assert result is False

result = td >= NaT
assert result is False

result = td < NaT
assert result is False

result = td <= NaT
assert result is False

result = NaT > td
assert result is False

result = NaT >= td
assert result is False

result = NaT < td
assert result is False

result = NaT <= td
assert result is False


class TestAddSubNaTMasking(object):
# TODO: parametrize over boxes
Expand Down