Skip to content

Commit d62f02b

Browse files
kawochenjreback
authored andcommitted
BUG: GH9907 generate_range when start and end have higher resolution than offset
1 parent 71ac2eb commit d62f02b

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

doc/source/whatsnew/v0.17.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ Bug Fixes
9090

9191

9292
- Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`)
93+
- Bug in ``offsets.generate_range`` where ``start`` and ``end`` have finer precision than ``offset`` (:issue:`9907`)
9394

9495

9596
- Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`)
9697

9798

9899

100+
99101
- Bug in ``test_categorical`` on big-endian builds (:issue:`10425`)
100102

101103

pandas/tseries/offsets.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2450,12 +2450,12 @@ def generate_range(start=None, end=None, periods=None,
24502450
if start and not offset.onOffset(start):
24512451
start = offset.rollforward(start)
24522452

2453-
if end and not offset.onOffset(end):
2453+
elif end and not offset.onOffset(end):
24542454
end = offset.rollback(end)
24552455

2456-
if periods is None and end < start:
2457-
end = None
2458-
periods = 0
2456+
if periods is None and end < start:
2457+
end = None
2458+
periods = 0
24592459

24602460
if end is None:
24612461
end = start + (periods - 1) * offset
@@ -2465,7 +2465,6 @@ def generate_range(start=None, end=None, periods=None,
24652465

24662466
cur = start
24672467

2468-
next_date = cur
24692468
while cur <= end:
24702469
yield cur
24712470

pandas/tseries/tests/test_daterange.py

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ def test_3(self):
5757
end=datetime(2008, 1, 6)),
5858
[])
5959

60+
def test_precision_finer_than_offset(self):
61+
# GH 9907
62+
result1 = DatetimeIndex(start='2015-04-15 00:00:03',
63+
end='2016-04-22 00:00:00', freq='Q')
64+
result2 = DatetimeIndex(start='2015-04-15 00:00:03',
65+
end='2015-06-22 00:00:04', freq='W')
66+
expected1_list = ['2015-06-30 00:00:03', '2015-09-30 00:00:03',
67+
'2015-12-31 00:00:03', '2016-03-31 00:00:03']
68+
expected2_list = ['2015-04-19 00:00:03', '2015-04-26 00:00:03',
69+
'2015-05-03 00:00:03', '2015-05-10 00:00:03',
70+
'2015-05-17 00:00:03', '2015-05-24 00:00:03',
71+
'2015-05-31 00:00:03', '2015-06-07 00:00:03',
72+
'2015-06-14 00:00:03', '2015-06-21 00:00:03']
73+
expected1 = DatetimeIndex(expected1_list, dtype='datetime64[ns]',
74+
freq='Q-DEC', tz=None)
75+
expected2 = DatetimeIndex(expected2_list, dtype='datetime64[ns]',
76+
freq='W-SUN', tz=None)
77+
self.assertTrue(result1.equals(expected1))
78+
self.assertTrue(result2.equals(expected2))
79+
6080

6181
class TestDateRange(tm.TestCase):
6282

0 commit comments

Comments
 (0)