Repository: pandas-dev/pandas

Issue:
BUG: pd.date_range(...) produces timestamps outside the specified range
Hi Pandas devs!

This call produces 3 timestamps when it has to produce only two:
    pd.date_range('2005-01-12 10:00', '2005-01-12 16:00', freq='345min')
this gives:
<class 'pandas.tseries.index.DatetimeIndex'>
[2005-01-12 10:00:00, ..., 2005-01-12 **21:30:00**]
Length: 3, Freq: 345T, Timezone: None

strangely, for another date function works as expected:
    pd.date_range('2005-01-13 10:00', '2005-01-13 16:00', freq='345min')
this gives:
<class 'pandas.tseries.index.DatetimeIndex'>
[2005-01-13 10:00:00, 2005-01-13 **15:45:00**]
Length: 2, Freq: 345T, Timezone: None

Could you please take a look? Thanks!

Pandas versions:
## INSTALLED VERSIONS

commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 30 Stepping 5, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US

pandas: 0.15.2
nose: 1.3.4
Cython: 0.22
numpy: 1.9.2
scipy: 0.15.1
statsmodels: 0.6.1
IPython: 3.0.0
sphinx: 1.2.3
patsy: 0.3.0
dateutil: 2.4.1
pytz: 2015.2
bottleneck: None
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.4.3
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.6.7
lxml: 3.4.2
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
rpy2: None
sqlalchemy: 0.9.9
pymysql: None
psycopg2: None



Discussion:
I can confirm this with master


Resolving patch:
diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py
--- a/pandas/tseries/index.py
+++ b/pandas/tseries/index.py
@@ -1,4 +1,5 @@
 # pylint: disable=E1101
+from __future__ import division
 import operator
 import warnings
 from datetime import time, datetime
@@ -1793,8 +1794,9 @@ def _generate_regular_range(start, end, periods, offset):
         stride = offset.nanos
         if periods is None:
             b = Timestamp(start).value
-            e = Timestamp(end).value
-            e += stride - e % stride
+            # cannot just use e = Timestamp(end) + 1 because arange breaks when
+            # stride is too large, see GH10887
+            e = b + (Timestamp(end).value - b)//stride * stride + stride//2
             # end.tz == start.tz by this point due to _generate implementation
             tz = start.tz
         elif start is not None:


Test patch:
diff --git a/pandas/tseries/tests/test_daterange.py b/pandas/tseries/tests/test_daterange.py
--- a/pandas/tseries/tests/test_daterange.py
+++ b/pandas/tseries/tests/test_daterange.py
@@ -490,6 +490,18 @@ def test_years_only(self):
         self.assertEqual(dr[0], datetime(2014, 1, 31))
         self.assertEqual(dr[-1], datetime(2014, 12, 31))
 
+    def test_freq_divides_end_in_nanos(self):
+        # GH 10885
+        result_1 = date_range('2005-01-12 10:00', '2005-01-12 16:00',
+                              freq='345min')
+        result_2 = date_range('2005-01-13 10:00', '2005-01-13 16:00',
+                              freq='345min')
+        expected_1 = DatetimeIndex(['2005-01-12 10:00:00', '2005-01-12 15:45:00'],
+                                   dtype='datetime64[ns]', freq='345T', tz=None)
+        expected_2 = DatetimeIndex(['2005-01-13 10:00:00', '2005-01-13 15:45:00'],
+                                   dtype='datetime64[ns]', freq='345T', tz=None)
+        self.assertTrue(result_1.equals(expected_1))
+        self.assertTrue(result_2.equals(expected_2))
 
 class TestCustomDateRange(tm.TestCase):