File tree 3 files changed +39
-8
lines changed
3 files changed +39
-8
lines changed Original file line number Diff line number Diff line change @@ -273,7 +273,7 @@ Datetimelike
273
273
Timedelta
274
274
^^^^^^^^^
275
275
276
- -
276
+ - Bug with comparisons between :class: ` Timedelta ` and `` NaT `` raising `` TypeError `` ( :issue: ` 26039 `)
277
277
-
278
278
-
279
279
Original file line number Diff line number Diff line change @@ -779,13 +779,16 @@ cdef class _Timedelta(timedelta):
779
779
return PyObject_RichCompare(np.array([self ]), other, op)
780
780
return PyObject_RichCompare(other, self , reverse_ops[op])
781
781
else :
782
- if op == Py_EQ:
783
- return False
784
- elif op == Py_NE:
785
- return True
786
- raise TypeError (' Cannot compare type {cls} with type {other}'
787
- .format(cls = type (self ).__name__,
788
- other = type (other).__name__))
782
+ if other is NaT:
783
+ return PyObject_RichCompare(other, self , reverse_ops[op])
784
+ else :
785
+ if op == Py_EQ:
786
+ return False
787
+ elif op == Py_NE:
788
+ return True
789
+ raise TypeError (' Cannot compare type {cls} with type {other}'
790
+ .format(cls = type (self ).__name__,
791
+ other = type (other).__name__))
789
792
790
793
return cmp_scalar(self .value, ots.value, op)
791
794
Original file line number Diff line number Diff line change @@ -441,6 +441,34 @@ def test_timedelta(self, freq):
441
441
tm .assert_index_equal (result1 , result4 )
442
442
tm .assert_index_equal (result2 , result3 )
443
443
444
+ def test_timedelta_nat_comparisons (self ):
445
+ # GH 26039
446
+ td = pd .Timedelta (0 )
447
+
448
+ result = td > NaT
449
+ assert result == False
450
+
451
+ result = td >= NaT
452
+ assert result == False
453
+
454
+ result = td < NaT
455
+ assert result == False
456
+
457
+ result = td <= NaT
458
+ assert result == False
459
+
460
+ result = NaT > td
461
+ assert result == False
462
+
463
+ result = NaT >= td
464
+ assert result == False
465
+
466
+ result = NaT < td
467
+ assert result == False
468
+
469
+ result = NaT <= td
470
+ assert result == False
471
+
444
472
445
473
class TestAddSubNaTMasking (object ):
446
474
# TODO: parametrize over boxes
You can’t perform that action at this time.
0 commit comments