From c1a4fbdb9f3ad2a261f90b87420d5b22c1558ae4 Mon Sep 17 00:00:00 2001 From: Garrett-R Date: Mon, 15 Dec 2014 00:04:41 -0800 Subject: [PATCH] Closes #8943 COMPAT: periods needs coercion to np.int64 In _generate_regular_range, we explicitly cast `periods` to np.int64 to prevent overflow in the function tseries.index._generate_regular_range. Note: we don't bother casting it for the function call generate_range since there's no danger of overflow in that function. --- doc/source/whatsnew/v0.16.0.txt | 3 +++ pandas/tests/test_index.py | 16 ++++++++++++++++ pandas/tseries/index.py | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.16.0.txt b/doc/source/whatsnew/v0.16.0.txt index a377b9caadc0c..74fcf2e1fbf06 100644 --- a/doc/source/whatsnew/v0.16.0.txt +++ b/doc/source/whatsnew/v0.16.0.txt @@ -48,3 +48,6 @@ Bug Fixes ~~~~~~~~~ .. _whatsnew_0160.bug_fixes: + +- Fixed compatibility issue in ``DatetimeIndex`` affecting architectures where ``numpy.int_`` defaults to ``numpy.int32`` (:issue:`8943`) + diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index c3b1ea6d742e3..575c4e8cb4140 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1909,6 +1909,22 @@ def test_time_loc(self): # GH8667 right.iloc[i] *= -10 tm.assert_series_equal(left, right) + def test_time_overflow_for_32bit_machines(self): + # GH8943. On some machines NumPy defaults to np.int32 (for example, + # 32-bit Linux machines). In the function _generate_regular_range + # found in tseries/index.py, `periods` gets multiplied by `strides` + # (which has value 1e9) and since the max value for np.int32 is ~2e9, + # and since those machines won't promote np.int32 to np.int64, we get + # overflow. + periods = np.int_(1000) + + idx1 = pd.date_range(start='2000', periods=periods, freq='S') + self.assertEqual(len(idx1), periods) + + idx2 = pd.date_range(end='2000', periods=periods, freq='S') + self.assertEqual(len(idx2), periods) + + class TestPeriodIndex(Base, tm.TestCase): _holder = PeriodIndex _multiprocess_can_split_ = True diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index edb6d34f65f5e..5e621761d63b6 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -1781,11 +1781,11 @@ def _generate_regular_range(start, end, periods, offset): tz = start.tz elif start is not None: b = Timestamp(start).value - e = b + periods * stride + e = b + np.int64(periods) * stride tz = start.tz elif end is not None: e = Timestamp(end).value + stride - b = e - periods * stride + b = e - np.int64(periods) * stride tz = end.tz else: raise NotImplementedError