From d66c5ecd6ae691791e84fb264cba514f67e28d5d Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Thu, 29 Jul 2021 12:40:05 -0400 Subject: [PATCH 1/2] REGR: nanosecond timestamp comparisons to OOB datetimes --- doc/source/whatsnew/v1.3.2.rst | 2 +- pandas/_libs/tslibs/timestamps.pyx | 4 ++-- pandas/tests/scalar/timestamp/test_comparisons.py | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.3.2.rst b/doc/source/whatsnew/v1.3.2.rst index f4804215db8c1..66e435ccd0319 100644 --- a/doc/source/whatsnew/v1.3.2.rst +++ b/doc/source/whatsnew/v1.3.2.rst @@ -18,7 +18,7 @@ Fixed regressions - Regression in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`) - Regression in :meth:`DataFrame.from_records` with empty records (:issue:`42456`) - Fixed regression in :meth:`DataFrame.shift` where TypeError occurred when shifting DataFrame created by concatenation of slices and fills with values (:issue:`42719`) -- +- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime`` objects outside the implementation bounds for nanosecond ``datetime`` (:issue:`42794`) .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index e4e9df5176459..fa86b7d9899af 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -270,9 +270,9 @@ cdef class _Timestamp(ABCTimestamp): if op == Py_EQ: return False if op == Py_LE or op == Py_LT: - return other.year <= self.year + return self.year <= other.year if op == Py_GE or op == Py_GT: - return other.year >= self.year + return self.year >= other.year cdef bint _can_compare(self, datetime other): if self.tzinfo is not None: diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index 555067f2aba1a..a4ebeb6d955c3 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -266,6 +266,15 @@ def test_timestamp_compare_oob_dt64(self): assert Timestamp.max < other + us # Note: numpy gets the reversed comparison wrong + # GH-42794 + other = datetime(9999, 9, 9) + assert Timestamp.min < other + assert Timestamp.max < other + + other = datetime(1, 1, 1) + assert Timestamp.max > other + assert Timestamp.min > other + def test_compare_zerodim_array(self): # GH#26916 ts = Timestamp.now() From 5827da4bcdaeaa50dee9768d6ff5e1ef33a74d79 Mon Sep 17 00:00:00 2001 From: Matthew Zeitlin Date: Thu, 29 Jul 2021 12:50:09 -0400 Subject: [PATCH 2/2] Add clip test too --- doc/source/whatsnew/v1.3.2.rst | 3 ++- pandas/tests/scalar/timestamp/test_comparisons.py | 4 ++++ pandas/tests/series/methods/test_clip.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.2.rst b/doc/source/whatsnew/v1.3.2.rst index 66e435ccd0319..b7e19004ddfd4 100644 --- a/doc/source/whatsnew/v1.3.2.rst +++ b/doc/source/whatsnew/v1.3.2.rst @@ -18,7 +18,8 @@ Fixed regressions - Regression in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`) - Regression in :meth:`DataFrame.from_records` with empty records (:issue:`42456`) - Fixed regression in :meth:`DataFrame.shift` where TypeError occurred when shifting DataFrame created by concatenation of slices and fills with values (:issue:`42719`) -- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime`` objects outside the implementation bounds for nanosecond ``datetime`` (:issue:`42794`) +- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index a4ebeb6d955c3..ee36223eb2496 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -269,11 +269,15 @@ def test_timestamp_compare_oob_dt64(self): # GH-42794 other = datetime(9999, 9, 9) assert Timestamp.min < other + assert other > Timestamp.min assert Timestamp.max < other + assert other > Timestamp.max other = datetime(1, 1, 1) assert Timestamp.max > other + assert other < Timestamp.max assert Timestamp.min > other + assert other < Timestamp.min def test_compare_zerodim_array(self): # GH#26916 diff --git a/pandas/tests/series/methods/test_clip.py b/pandas/tests/series/methods/test_clip.py index e4803a9cd3038..620f529b522ae 100644 --- a/pandas/tests/series/methods/test_clip.py +++ b/pandas/tests/series/methods/test_clip.py @@ -1,3 +1,5 @@ +from datetime import datetime + import numpy as np import pytest @@ -128,6 +130,15 @@ def test_clip_with_datetimes(self): ) tm.assert_series_equal(result, expected) + def test_clip_with_timestamps_and_oob_datetimes(self): + # GH-42794 + ser = Series([datetime(1, 1, 1), datetime(9999, 9, 9)]) + + result = ser.clip(lower=Timestamp.min, upper=Timestamp.max) + expected = Series([Timestamp.min, Timestamp.max], dtype="object") + + tm.assert_series_equal(result, expected) + def test_clip_pos_args_deprecation(self): # https://github.com/pandas-dev/pandas/issues/41485 ser = Series([1, 2, 3])