Skip to content

Commit 9e5573e

Browse files
authored
BUG: date_range hitting iNaT (pandas-dev#39308)
1 parent 7e4d331 commit 9e5573e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Datetimelike
233233
- Bug in :meth:`DatetimeIndex.intersection`, :meth:`DatetimeIndex.symmetric_difference`, :meth:`PeriodIndex.intersection`, :meth:`PeriodIndex.symmetric_difference` always returning object-dtype when operating with :class:`CategoricalIndex` (:issue:`38741`)
234234
- Bug in :meth:`Series.where` incorrectly casting ``datetime64`` values to ``int64`` (:issue:`37682`)
235235
- Bug in :class:`Categorical` incorrectly typecasting ``datetime`` object to ``Timestamp`` (:issue:`38878`)
236+
- Bug in :func:`date_range` incorrectly creating :class:`DatetimeIndex` containing ``NaT`` instead of raising ``OutOfBoundsDatetime`` in corner cases (:issue:`24124`)
236237

237238
Timedelta
238239
^^^^^^^^^

pandas/core/arrays/_ranges.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
import numpy as np
99

10-
from pandas._libs.tslibs import BaseOffset, OutOfBoundsDatetime, Timedelta, Timestamp
10+
from pandas._libs.tslibs import (
11+
BaseOffset,
12+
OutOfBoundsDatetime,
13+
Timedelta,
14+
Timestamp,
15+
iNaT,
16+
)
1117

1218

1319
def generate_regular_range(
@@ -150,7 +156,12 @@ def _generate_range_overflow_safe_signed(
150156
addend = np.int64(periods) * np.int64(stride)
151157
try:
152158
# easy case with no overflows
153-
return np.int64(endpoint) + addend
159+
result = np.int64(endpoint) + addend
160+
if result == iNaT:
161+
# Putting this into a DatetimeArray/TimedeltaArray
162+
# would incorrectly be interpreted as NaT
163+
raise OverflowError
164+
return result
154165
except (FloatingPointError, OverflowError):
155166
# with endpoint negative and addend positive we risk
156167
# FloatingPointError; with reversed signed we risk OverflowError

pandas/tests/indexes/datetimes/test_date_range.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pandas.util._test_decorators as td
1616

1717
import pandas as pd
18-
from pandas import DatetimeIndex, Timestamp, bdate_range, date_range, offsets
18+
from pandas import DatetimeIndex, Timedelta, Timestamp, bdate_range, date_range, offsets
1919
import pandas._testing as tm
2020
from pandas.core.arrays.datetimes import generate_range
2121

@@ -76,6 +76,13 @@ def test_date_range_timestamp_equiv_preserve_frequency(self):
7676

7777

7878
class TestDateRanges:
79+
def test_date_range_near_implementation_bound(self):
80+
# GH#???
81+
freq = Timedelta(1)
82+
83+
with pytest.raises(OutOfBoundsDatetime, match="Cannot generate range with"):
84+
date_range(end=Timestamp.min, periods=2, freq=freq)
85+
7986
def test_date_range_nat(self):
8087
# GH#11587
8188
msg = "Neither `start` nor `end` can be NaT"

0 commit comments

Comments
 (0)