Skip to content

solve "Int64 with null value mangles large-ish integers" problem #30282

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

Closed
wants to merge 2 commits into from
Closed
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.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@ ExtensionArray
- Fixed bug where the constructor of :class:`DataFrame` with ``dtype='string'`` would fail (:issue:`27953`, :issue:`33623`)
- Bug where :class:`DataFrame` column set to scalar extension type was considered an object type rather than the extension type (:issue:`34832`)
- Fixed bug in :meth:`IntegerArray.astype` to correctly copy the mask as well (:issue:`34931`).
- Bug in creating nullable integer ``Series`` while using large-ish integers with ``NaN`` values (:issue:`30268`)

Other
^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ def coerce_to_array(
mask = mask.copy()
return values, mask

values = np.array(values, copy=copy)
values = np.asarray(values, dtype=getattr(values, "dtype", object))
if copy and hasattr(values, "dtype"):
values = values.copy()

if is_object_dtype(values):
inferred_type = lib.infer_dtype(values, skipna=True)
if inferred_type == "empty":
Expand All @@ -239,6 +242,7 @@ def coerce_to_array(
"mixed-integer",
"integer-na",
"mixed-integer-float",
"boolean",
]:
raise TypeError(f"{values.dtype} cannot be converted to an IntegerDtype")

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/arrays/integer/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,19 @@ def test_to_integer_array(values, to_dtype, result_dtype):
assert result.dtype == result_dtype()
expected = integer_array(values, dtype=result_dtype())
tm.assert_extension_array_equal(result, expected)


def test_construction_large_numbers():
# issue 30268
values = [9999999999999999, 123123123123123123, 10000000000000543, np.nan]
expected = pd.Series(
IntegerArray(
np.array(
[9999999999999999, 123123123123123123, 10000000000000543, 0],
dtype="int64",
),
np.array([False, False, False, True]),
)
)
result = pd.Series(values, dtype="Int64")
tm.assert_series_equal(result, expected, check_exact=True)