Skip to content

Commit 032c01e

Browse files
Backport PR #39196: REGR: NaT.__richmp__(dateobj) (#39201)
Co-authored-by: jbrockmendel <[email protected]>
1 parent b12df46 commit 032c01e

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

doc/source/whatsnew/v1.2.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Fixed regressions
3030
- Fixed regression that raised ``AttributeError`` with PyArrow versions [0.16.0, 1.0.0) (:issue:`38801`)
3131
- Fixed regression in :meth:`DataFrame.groupby` when aggregating an :class:`ExtensionDType` that could fail for non-numeric values (:issue:`38980`)
3232
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``KeyError`` with :class:`MultiIndex` and list-like columns indexer enlarging :class:`DataFrame` (:issue:`39147`)
33-
-
33+
- Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`)
3434

3535
.. ---------------------------------------------------------------------------
3636

pandas/_libs/tslibs/nattype.pyx

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import warnings
2+
13
from cpython.datetime cimport (
4+
PyDate_Check,
25
PyDateTime_Check,
36
PyDateTime_IMPORT,
47
PyDelta_Check,
@@ -125,6 +128,21 @@ cdef class _NaT(datetime):
125128
return NotImplemented
126129
return result
127130

131+
elif PyDate_Check(other):
132+
# GH#39151 don't defer to datetime.date object
133+
if op == Py_EQ:
134+
return False
135+
if op == Py_NE:
136+
return True
137+
warnings.warn(
138+
"Comparison of NaT with datetime.date is deprecated in "
139+
"order to match the standard library behavior. "
140+
"In a future version these will be considered non-comparable.",
141+
FutureWarning,
142+
stacklevel=1,
143+
)
144+
return False
145+
128146
return NotImplemented
129147

130148
def __add__(self, other):

pandas/tests/scalar/test_nat.py

+34
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,40 @@ def test_nat_comparisons_invalid(other, op):
575575
op(other, NaT)
576576

577577

578+
def test_compare_date():
579+
# GH#39151 comparing NaT with date object is deprecated
580+
# See also: tests.scalar.timestamps.test_comparisons::test_compare_date
581+
582+
dt = Timestamp.now().to_pydatetime().date()
583+
584+
for left, right in [(NaT, dt), (dt, NaT)]:
585+
assert not left == right
586+
assert left != right
587+
588+
with tm.assert_produces_warning(FutureWarning):
589+
assert not left < right
590+
with tm.assert_produces_warning(FutureWarning):
591+
assert not left <= right
592+
with tm.assert_produces_warning(FutureWarning):
593+
assert not left > right
594+
with tm.assert_produces_warning(FutureWarning):
595+
assert not left >= right
596+
597+
# Once the deprecation is enforced, the following assertions
598+
# can be enabled:
599+
# assert not left == right
600+
# assert left != right
601+
#
602+
# with pytest.raises(TypeError):
603+
# left < right
604+
# with pytest.raises(TypeError):
605+
# left <= right
606+
# with pytest.raises(TypeError):
607+
# left > right
608+
# with pytest.raises(TypeError):
609+
# left >= right
610+
611+
578612
@pytest.mark.parametrize(
579613
"obj",
580614
[

0 commit comments

Comments
 (0)