Skip to content

Commit 6b48b58

Browse files
committed
Merge pull request #10887 from kawochen/BUG-FIX-10885
BUG: GH10885 where an edge case in date_range produces an extra point
2 parents c06ce81 + ce349d1 commit 6b48b58

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ Bug Fixes
781781
- Bug in ``DatetimeIndex.take`` and ``TimedeltaIndex.take`` may not raise ``IndexError`` against invalid index (:issue:`10295`)
782782
- Bug in ``Series([np.nan]).astype('M8[ms]')``, which now returns ``Series([pd.NaT])`` (:issue:`10747`)
783783
- Bug in ``PeriodIndex.order`` reset freq (:issue:`10295`)
784+
- Bug in ``date_range`` when ``freq`` divides ``end`` as nanos (:issue:`10885`)
784785
- Bug in ``iloc`` allowing memory outside bounds of a Series to be accessed with negative integers (:issue:`10779`)
785786
- Bug in ``read_msgpack`` where encoding is not respected (:issue:`10580`)
786787
- Bug preventing access to the first index when using ``iloc`` with a list containing the appropriate negative integer (:issue:`10547`, :issue:`10779`)

pandas/tseries/index.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=E1101
2+
from __future__ import division
23
import operator
34
import warnings
45
from datetime import time, datetime
@@ -1793,8 +1794,9 @@ def _generate_regular_range(start, end, periods, offset):
17931794
stride = offset.nanos
17941795
if periods is None:
17951796
b = Timestamp(start).value
1796-
e = Timestamp(end).value
1797-
e += stride - e % stride
1797+
# cannot just use e = Timestamp(end) + 1 because arange breaks when
1798+
# stride is too large, see GH10887
1799+
e = b + (Timestamp(end).value - b)//stride * stride + stride//2
17981800
# end.tz == start.tz by this point due to _generate implementation
17991801
tz = start.tz
18001802
elif start is not None:

pandas/tseries/tests/test_daterange.py

+12
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,18 @@ def test_years_only(self):
490490
self.assertEqual(dr[0], datetime(2014, 1, 31))
491491
self.assertEqual(dr[-1], datetime(2014, 12, 31))
492492

493+
def test_freq_divides_end_in_nanos(self):
494+
# GH 10885
495+
result_1 = date_range('2005-01-12 10:00', '2005-01-12 16:00',
496+
freq='345min')
497+
result_2 = date_range('2005-01-13 10:00', '2005-01-13 16:00',
498+
freq='345min')
499+
expected_1 = DatetimeIndex(['2005-01-12 10:00:00', '2005-01-12 15:45:00'],
500+
dtype='datetime64[ns]', freq='345T', tz=None)
501+
expected_2 = DatetimeIndex(['2005-01-13 10:00:00', '2005-01-13 15:45:00'],
502+
dtype='datetime64[ns]', freq='345T', tz=None)
503+
self.assertTrue(result_1.equals(expected_1))
504+
self.assertTrue(result_2.equals(expected_2))
493505

494506
class TestCustomDateRange(tm.TestCase):
495507

0 commit comments

Comments
 (0)