Skip to content

CLN: astype_nansafe require dtype object #38466

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
Dec 14, 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
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ def astype(self, dtype=None, copy=True):
else:
return self.copy()
dtype = self.dtype.update_dtype(dtype)
subtype = dtype._subtype_with_str
subtype = pandas_dtype(dtype._subtype_with_str)
# TODO copy=False is broken for astype_nansafe with int -> float, so cannot
# passthrough copy keyword: https://github.com/pandas-dev/pandas/issues/34456
sp_values = astype_nansafe(self.sp_values, subtype, copy=True)
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
is_timedelta64_dtype,
is_timedelta64_ns_dtype,
is_unsigned_integer_dtype,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
Expand Down Expand Up @@ -965,7 +964,7 @@ def astype_nansafe(
Parameters
----------
arr : ndarray
dtype : np.dtype
dtype : np.dtype or ExtensionDtype
copy : bool, default True
If False, a view will be attempted but may fail, if
e.g. the item sizes don't align.
Expand All @@ -978,11 +977,11 @@ def astype_nansafe(
The dtype was a datetime64/timedelta64 dtype, but it had no unit.
"""
# dispatch on extension dtype if needed
if is_extension_array_dtype(dtype):
if isinstance(dtype, ExtensionDtype):
return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)

if not isinstance(dtype, np.dtype):
dtype = pandas_dtype(dtype)
elif not isinstance(dtype, np.dtype):
raise ValueError("dtype must be np.dtype or ExtensionDtype")

if issubclass(dtype.type, str):
return lib.ensure_string_array(
Expand Down
1 change: 1 addition & 0 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,7 @@ def _convert_to_ndarrays(
except (AttributeError, TypeError):
# invalid input to is_bool_dtype
pass
cast_type = pandas_dtype(cast_type)
cvals = self._cast_types(cvals, cast_type, c)

result[c] = cvals
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ def test__is_dtype_type(input_param, result):
def test_astype_nansafe(val, typ):
arr = np.array([val])

typ = np.dtype(typ)

msg = "Cannot convert NaT values to integer"
with pytest.raises(ValueError, match=msg):
astype_nansafe(arr, dtype=typ)
Expand All @@ -738,14 +740,16 @@ def test_astype_nansafe(val, typ):
def test_astype_datetime64_bad_dtype_raises(from_type, to_type):
arr = np.array([from_type("2018")])

to_type = np.dtype(to_type)

with pytest.raises(TypeError, match="cannot astype"):
astype_nansafe(arr, dtype=to_type)


@pytest.mark.parametrize("from_type", [np.datetime64, np.timedelta64])
def test_astype_object_preserves_datetime_na(from_type):
arr = np.array([from_type("NaT")])
result = astype_nansafe(arr, dtype="object")
result = astype_nansafe(arr, dtype=np.dtype("object"))

assert isna(result)[0]

Expand Down