Skip to content

Commit 890734d

Browse files
committed
Merge pull request #9078 from Garrett-R/fix_8943
Closes #8943: COMPAT: periods needs coercion to np.int64
2 parents e92df22 + c1a4fbd commit 890734d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v0.16.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ Bug Fixes
4848
~~~~~~~~~
4949

5050
.. _whatsnew_0160.bug_fixes:
51+
52+
- Fixed compatibility issue in ``DatetimeIndex`` affecting architectures where ``numpy.int_`` defaults to ``numpy.int32`` (:issue:`8943`)
53+

pandas/tests/test_index.py

+16
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,22 @@ def test_time_loc(self): # GH8667
19091909
right.iloc[i] *= -10
19101910
tm.assert_series_equal(left, right)
19111911

1912+
def test_time_overflow_for_32bit_machines(self):
1913+
# GH8943. On some machines NumPy defaults to np.int32 (for example,
1914+
# 32-bit Linux machines). In the function _generate_regular_range
1915+
# found in tseries/index.py, `periods` gets multiplied by `strides`
1916+
# (which has value 1e9) and since the max value for np.int32 is ~2e9,
1917+
# and since those machines won't promote np.int32 to np.int64, we get
1918+
# overflow.
1919+
periods = np.int_(1000)
1920+
1921+
idx1 = pd.date_range(start='2000', periods=periods, freq='S')
1922+
self.assertEqual(len(idx1), periods)
1923+
1924+
idx2 = pd.date_range(end='2000', periods=periods, freq='S')
1925+
self.assertEqual(len(idx2), periods)
1926+
1927+
19121928
class TestPeriodIndex(Base, tm.TestCase):
19131929
_holder = PeriodIndex
19141930
_multiprocess_can_split_ = True

pandas/tseries/index.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1781,11 +1781,11 @@ def _generate_regular_range(start, end, periods, offset):
17811781
tz = start.tz
17821782
elif start is not None:
17831783
b = Timestamp(start).value
1784-
e = b + periods * stride
1784+
e = b + np.int64(periods) * stride
17851785
tz = start.tz
17861786
elif end is not None:
17871787
e = Timestamp(end).value + stride
1788-
b = e - periods * stride
1788+
b = e - np.int64(periods) * stride
17891789
tz = end.tz
17901790
else:
17911791
raise NotImplementedError

0 commit comments

Comments
 (0)