Skip to content

Commit a4c5d78

Browse files
authored
REGR: nanosecond timestamp comparisons to OOB datetimes (#42796)
1 parent 4a2096d commit a4c5d78

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Regression in :meth:`DataFrame.from_records` with empty records (:issue:`42456`)
2020
- Fixed regression in :meth:`DataFrame.shift` where TypeError occurred when shifting DataFrame created by concatenation of slices and fills with values (:issue:`42719`)
2121
- Regression in :meth:`DataFrame.agg` when the ``func`` argument returned lists and ``axis=1`` (:issue:`42727`)
22+
- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`)
2223
-
2324

2425
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/timestamps.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ cdef class _Timestamp(ABCTimestamp):
270270
if op == Py_EQ:
271271
return False
272272
if op == Py_LE or op == Py_LT:
273-
return other.year <= self.year
273+
return self.year <= other.year
274274
if op == Py_GE or op == Py_GT:
275-
return other.year >= self.year
275+
return self.year >= other.year
276276

277277
cdef bint _can_compare(self, datetime other):
278278
if self.tzinfo is not None:

pandas/tests/scalar/timestamp/test_comparisons.py

+13
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,19 @@ def test_timestamp_compare_oob_dt64(self):
266266
assert Timestamp.max < other + us
267267
# Note: numpy gets the reversed comparison wrong
268268

269+
# GH-42794
270+
other = datetime(9999, 9, 9)
271+
assert Timestamp.min < other
272+
assert other > Timestamp.min
273+
assert Timestamp.max < other
274+
assert other > Timestamp.max
275+
276+
other = datetime(1, 1, 1)
277+
assert Timestamp.max > other
278+
assert other < Timestamp.max
279+
assert Timestamp.min > other
280+
assert other < Timestamp.min
281+
269282
def test_compare_zerodim_array(self):
270283
# GH#26916
271284
ts = Timestamp.now()

pandas/tests/series/methods/test_clip.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import datetime
2+
13
import numpy as np
24
import pytest
35

@@ -128,6 +130,15 @@ def test_clip_with_datetimes(self):
128130
)
129131
tm.assert_series_equal(result, expected)
130132

133+
def test_clip_with_timestamps_and_oob_datetimes(self):
134+
# GH-42794
135+
ser = Series([datetime(1, 1, 1), datetime(9999, 9, 9)])
136+
137+
result = ser.clip(lower=Timestamp.min, upper=Timestamp.max)
138+
expected = Series([Timestamp.min, Timestamp.max], dtype="object")
139+
140+
tm.assert_series_equal(result, expected)
141+
131142
def test_clip_pos_args_deprecation(self):
132143
# https://github.com/pandas-dev/pandas/issues/41485
133144
ser = Series([1, 2, 3])

0 commit comments

Comments
 (0)