Skip to content

Commit 21ed2e8

Browse files
Backport PR pandas-dev#36316: BUG: Don't overflow with large int scalar (pandas-dev#36334)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 2000334 commit 21ed2e8

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
@@ -1474,3 +1474,10 @@ def test_construction_from_ordered_collection(self):
14741474
result = Series({"a": 1, "b": 2}.values())
14751475
expected = Series([1, 2])
14761476
tm.assert_series_equal(result, expected)
1477+
1478+
def test_construction_from_large_int_scalar_no_overflow(self):
1479+
# https://github.com/pandas-dev/pandas/issues/36291
1480+
n = 1_000_000_000_000_000_000_000
1481+
result = Series(n, index=[0])
1482+
expected = Series(n)
1483+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)