From bbf68e500228b1c397a079164abd36fdecedd249 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 12 Sep 2020 17:57:10 -0500 Subject: [PATCH 1/2] BUG: Don't overflow with large int scalar --- doc/source/whatsnew/v1.1.3.rst | 1 + pandas/core/dtypes/cast.py | 5 +++++ pandas/tests/series/test_constructors.py | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/doc/source/whatsnew/v1.1.3.rst b/doc/source/whatsnew/v1.1.3.rst index 25d223418fc92..5cbd160f29d66 100644 --- a/doc/source/whatsnew/v1.1.3.rst +++ b/doc/source/whatsnew/v1.1.3.rst @@ -25,6 +25,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in :meth:`Series.str.startswith` and :meth:`Series.str.endswith` with ``category`` dtype not propagating ``na`` parameter (:issue:`36241`) +- Bug in :class:`Series` constructor where integer overflow would occur for sufficiently large scalar inputs when an index was provided (:issue:`36291`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 64ccc0be0a25d..db54adafd6870 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -697,6 +697,11 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, else: dtype = np.dtype(np.int64) + try: + np.array([val], dtype=dtype) + except OverflowError: + dtype = np.array([val]).dtype + elif is_float(val): if isinstance(val, np.floating): dtype = np.dtype(type(val)) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 0fb8c5955a2e7..8ac0a55e63cd1 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1501,3 +1501,10 @@ def test_construction_from_ordered_collection(self): result = Series({"a": 1, "b": 2}.values()) expected = Series([1, 2]) tm.assert_series_equal(result, expected) + + def test_construction_from_large_int_scalar_no_overflow(self): + # https://github.com/pandas-dev/pandas/issues/36291 + n = 1_000_000_000_000_000_000_000 + result = Series(n, index=[0]) + expected = Series(n) + tm.assert_series_equal(result, expected) From c5a27442dac61bdc6a5c0cdbac306fa0cffe8545 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 12 Sep 2020 18:05:06 -0500 Subject: [PATCH 2/2] nit --- pandas/core/dtypes/cast.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index db54adafd6870..05759ffb43dde 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -698,9 +698,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, dtype = np.dtype(np.int64) try: - np.array([val], dtype=dtype) + np.array(val, dtype=dtype) except OverflowError: - dtype = np.array([val]).dtype + dtype = np.array(val).dtype elif is_float(val): if isinstance(val, np.floating):