Skip to content

Commit a3b5c49

Browse files
authored
REGR: NaT.__richmp__(dateobj) (pandas-dev#39196)
1 parent a4c2d17 commit a3b5c49

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
@@ -31,7 +31,7 @@ Fixed regressions
3131
- Fixed regression that raised ``AttributeError`` with PyArrow versions [0.16.0, 1.0.0) (:issue:`38801`)
3232
- Fixed regression in :meth:`DataFrame.groupby` when aggregating an :class:`ExtensionDType` that could fail for non-numeric values (:issue:`38980`)
3333
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``KeyError`` with :class:`MultiIndex` and list-like columns indexer enlarging :class:`DataFrame` (:issue:`39147`)
34-
-
34+
- Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`)
3535

3636
.. ---------------------------------------------------------------------------
3737

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
@@ -584,6 +584,40 @@ def test_nat_comparisons_invalid(other_and_type, symbol_and_op):
584584
op(other, NaT)
585585

586586

587+
def test_compare_date():
588+
# GH#39151 comparing NaT with date object is deprecated
589+
# See also: tests.scalar.timestamps.test_comparisons::test_compare_date
590+
591+
dt = Timestamp.now().to_pydatetime().date()
592+
593+
for left, right in [(NaT, dt), (dt, NaT)]:
594+
assert not left == right
595+
assert left != right
596+
597+
with tm.assert_produces_warning(FutureWarning):
598+
assert not left < right
599+
with tm.assert_produces_warning(FutureWarning):
600+
assert not left <= right
601+
with tm.assert_produces_warning(FutureWarning):
602+
assert not left > right
603+
with tm.assert_produces_warning(FutureWarning):
604+
assert not left >= right
605+
606+
# Once the deprecation is enforced, the following assertions
607+
# can be enabled:
608+
# assert not left == right
609+
# assert left != right
610+
#
611+
# with pytest.raises(TypeError):
612+
# left < right
613+
# with pytest.raises(TypeError):
614+
# left <= right
615+
# with pytest.raises(TypeError):
616+
# left > right
617+
# with pytest.raises(TypeError):
618+
# left >= right
619+
620+
587621
@pytest.mark.parametrize(
588622
"obj",
589623
[

0 commit comments

Comments
 (0)