Skip to content

REGR: NaT.__richmp__(dateobj) #39196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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/v1.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Fixed regressions
- Fixed regression that raised ``AttributeError`` with PyArrow versions [0.16.0, 1.0.0) (:issue:`38801`)
- Fixed regression in :meth:`DataFrame.groupby` when aggregating an :class:`ExtensionDType` that could fail for non-numeric values (:issue:`38980`)
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``KeyError`` with :class:`MultiIndex` and list-like columns indexer enlarging :class:`DataFrame` (:issue:`39147`)
-
- Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`)

.. ---------------------------------------------------------------------------

Expand Down
18 changes: 18 additions & 0 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import warnings

from cpython.datetime cimport (
PyDate_Check,
PyDateTime_Check,
PyDateTime_IMPORT,
PyDelta_Check,
Expand Down Expand Up @@ -125,6 +128,21 @@ cdef class _NaT(datetime):
return NotImplemented
return result

elif PyDate_Check(other):
# GH#39151 don't defer to datetime.date object
if op == Py_EQ:
return False
if op == Py_NE:
return True
warnings.warn(
"Comparison of NaT with datetime.date is deprecated in "
"order to match the standard library behavior. "
"In a future version these will be considered non-comparable.",
FutureWarning,
stacklevel=1,
)
return False

return NotImplemented

def __add__(self, other):
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,40 @@ def test_nat_comparisons_invalid(other_and_type, symbol_and_op):
op(other, NaT)


def test_compare_date():
# GH#39151 comparing NaT with date object is deprecated
# See also: tests.scalar.timestamps.test_comparisons::test_compare_date

dt = Timestamp.now().to_pydatetime().date()

for left, right in [(NaT, dt), (dt, NaT)]:
assert not left == right
assert left != right

with tm.assert_produces_warning(FutureWarning):
assert not left < right
with tm.assert_produces_warning(FutureWarning):
assert not left <= right
with tm.assert_produces_warning(FutureWarning):
assert not left > right
with tm.assert_produces_warning(FutureWarning):
assert not left >= right

# Once the deprecation is enforced, the following assertions
# can be enabled:
# assert not left == right
# assert left != right
#
# with pytest.raises(TypeError):
# left < right
# with pytest.raises(TypeError):
# left <= right
# with pytest.raises(TypeError):
# left > right
# with pytest.raises(TypeError):
# left >= right


@pytest.mark.parametrize(
"obj",
[
Expand Down