Skip to content

BUG: Don't overflow with large int scalar #36316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)