Skip to content

BUG: make Series handle dtype='int64' for string array #48333

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 5 commits into from
Sep 12, 2022
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Numeric

Conversion
^^^^^^^^^^
-
- Bug in constructing :class:`Series` with ``int64`` dtype from a string list raising instead of casting (:issue:`44923`)
-

Strings
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 @@ -1851,6 +1851,11 @@ def maybe_cast_to_integer_array(
# doesn't handle `uint64` correctly.
arr = np.asarray(arr)

if np.issubdtype(arr.dtype, str):
if (casted.astype(str) == arr).all():
return casted
raise ValueError(f"string values cannot be losslessly cast to {dtype}")

if is_unsigned_integer_dtype(dtype) and (arr < 0).any():
raise OverflowError("Trying to coerce negative values to unsigned integers")

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,19 @@ def test_constructor_bool_dtype_missing_values(self):
expected = Series(True, index=[0], dtype="bool")
tm.assert_series_equal(result, expected)

def test_constructor_int64_dtype(self, any_int_dtype):
# GH#44923
result = Series(["0", "1", "2"], dtype=any_int_dtype)
expected = Series([0, 1, 2], dtype=any_int_dtype)
tm.assert_series_equal(result, expected)

def test_constructor_raise_on_lossy_conversion_of_strings(self):
# GH#44923
with pytest.raises(
ValueError, match="string values cannot be losslessly cast to int8"
):
Series(["128"], dtype="int8")

def test_constructor_dtype_timedelta_alternative_construct(self):
# GH#35465
result = Series([1000000, 200000, 3000000], dtype="timedelta64[ns]")
Expand Down