Skip to content

BUG: PandasArray.astype use astype_nansafe #51174

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 1 commit into from
Feb 6, 2023
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
16 changes: 16 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
)
from pandas.compat.numpy import function as nv

from pandas.core.dtypes.astype import astype_array
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
from pandas.core.dtypes.common import (
is_dtype_equal,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import PandasDtype
from pandas.core.dtypes.missing import isna

Expand Down Expand Up @@ -185,6 +190,17 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
# ------------------------------------------------------------------------
# Pandas ExtensionArray Interface

def astype(self, dtype, copy: bool = True):
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if copy:
return self.copy()
return self

result = astype_array(self._ndarray, dtype=dtype, copy=copy)
return result

def isna(self) -> np.ndarray:
return isna(self._ndarray)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ def astype(self, dtype, copy: bool = True):
values = arr.astype(dtype.numpy_dtype)
return FloatingArray(values, mask, copy=False)
elif isinstance(dtype, ExtensionDtype):
return super().astype(dtype, copy=copy)
# Skip the PandasArray.astype method
return ExtensionArray.astype(self, dtype, copy)
elif np.issubdtype(dtype, np.floating):
arr = self._ndarray.copy()
mask = self.isna()
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/extension/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ def assert_series_equal(cls, left, right, *args, **kwargs):


class TestCasting(BaseNumPyTests, base.BaseCastingTests):
@skip_nested
def test_astype_str(self, data):
# ValueError: setting an array element with a sequence
super().test_astype_str(data)
pass


class TestConstructors(BaseNumPyTests, base.BaseConstructorsTests):
Expand Down
17 changes: 14 additions & 3 deletions pandas/tests/series/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,25 @@ def test_astype_cast_object_int_fail(self, dtype):
def test_astype_float_to_uint_negatives_raise(
self, float_numpy_dtype, any_unsigned_int_numpy_dtype
):
# GH#45151
# TODO: same for EA float/uint dtypes
# GH#45151 We don't cast negative numbers to nonsense values
# TODO: same for EA float/uint dtypes, signed integers?
arr = np.arange(5).astype(float_numpy_dtype) - 3 # includes negatives
ser = Series(arr)

with pytest.raises(ValueError, match="losslessly"):
msg = "Cannot losslessly cast from .* to .*"
with pytest.raises(ValueError, match=msg):
ser.astype(any_unsigned_int_numpy_dtype)

with pytest.raises(ValueError, match=msg):
ser.to_frame().astype(any_unsigned_int_numpy_dtype)

with pytest.raises(ValueError, match=msg):
# We currently catch and re-raise in Index.astype
Index(ser).astype(any_unsigned_int_numpy_dtype)

with pytest.raises(ValueError, match=msg):
ser.array.astype(any_unsigned_int_numpy_dtype)

def test_astype_cast_object_int(self):
arr = Series(["1", "2", "3", "4"], dtype=object)
result = arr.astype(int)
Expand Down