Skip to content

Commit e200f32

Browse files
committed
BUG: avoid overflow in Bday generate_range, closes pandas-dev#24252
1 parent c07d71d commit e200f32

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pandas/tests/indexes/datetimes/test_date_range.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ def test_bdays_and_open_boundaries(self, closed):
740740
expected = pd.date_range(bday_start, bday_end, freq='D')
741741
tm.assert_index_equal(result, expected)
742742

743+
def test_bday_near_overflow(self):
744+
# GH#24252 avoid doing unnecessary addition that _would_ overflow
745+
start = pd.Timestamp.max.floor("D").to_pydatetime()
746+
rng = pd.date_range(start, end=None, periods=1, freq='B')
747+
expected = pd.DatetimeIndex([start], freq='B')
748+
tm.assert_index_equal(rng, expected)
749+
743750

744751
class TestCustomDateRange:
745752

pandas/tseries/offsets.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,11 @@ def generate_range(start=None, end=None, periods=None, offset=BDay()):
24672467
while cur <= end:
24682468
yield cur
24692469

2470+
if cur == end:
2471+
# GH#24252 avoid overflows by not performing the addition
2472+
# in offset.apply unless we have to
2473+
break
2474+
24702475
# faster than cur + offset
24712476
next_date = offset.apply(cur)
24722477
if next_date <= cur:
@@ -2477,6 +2482,11 @@ def generate_range(start=None, end=None, periods=None, offset=BDay()):
24772482
while cur >= end:
24782483
yield cur
24792484

2485+
if cur == end:
2486+
# GH#24252 avoid overflows by not performing the addition
2487+
# in offset.apply unless we have to
2488+
break
2489+
24802490
# faster than cur + offset
24812491
next_date = offset.apply(cur)
24822492
if next_date >= cur:

0 commit comments

Comments
 (0)