Skip to content

Backport PR #42796 on branch 1.3.x (REGR: nanosecond timestamp comparisons to OOB datetimes) #42806

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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Fixed regressions
- 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`)
- Regression in :meth:`DataFrame.agg` when the ``func`` argument returned lists and ``axis=1`` (:issue:`42727`)
- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`)
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/scalar/timestamp/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ 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 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
ts = Timestamp.now()
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_clip.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

import numpy as np
import pytest

Expand Down Expand Up @@ -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])
Expand Down