Skip to content

Commit 22374c3

Browse files
authored
BUG: Don't overflow with large int scalar (#36316)
1 parent 23e28df commit 22374c3

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed regressions
2525
Bug fixes
2626
~~~~~~~~~
2727
- Bug in :meth:`Series.str.startswith` and :meth:`Series.str.endswith` with ``category`` dtype not propagating ``na`` parameter (:issue:`36241`)
28+
- Bug in :class:`Series` constructor where integer overflow would occur for sufficiently large scalar inputs when an index was provided (:issue:`36291`)
2829

2930
.. ---------------------------------------------------------------------------
3031

pandas/core/dtypes/cast.py

+5
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,11 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
697697
else:
698698
dtype = np.dtype(np.int64)
699699

700+
try:
701+
np.array(val, dtype=dtype)
702+
except OverflowError:
703+
dtype = np.array(val).dtype
704+
700705
elif is_float(val):
701706
if isinstance(val, np.floating):
702707
dtype = np.dtype(type(val))

pandas/tests/series/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -1501,3 +1501,10 @@ def test_construction_from_ordered_collection(self):
15011501
result = Series({"a": 1, "b": 2}.values())
15021502
expected = Series([1, 2])
15031503
tm.assert_series_equal(result, expected)
1504+
1505+
def test_construction_from_large_int_scalar_no_overflow(self):
1506+
# https://github.com/pandas-dev/pandas/issues/36291
1507+
n = 1_000_000_000_000_000_000_000
1508+
result = Series(n, index=[0])
1509+
expected = Series(n)
1510+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)