Skip to content

Commit 5b3f50b

Browse files
authored
CLN: astype_nansafe require dtype object (#38466)
1 parent 8e4fe37 commit 5b3f50b

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ def astype(self, dtype=None, copy=True):
10611061
else:
10621062
return self.copy()
10631063
dtype = self.dtype.update_dtype(dtype)
1064-
subtype = dtype._subtype_with_str
1064+
subtype = pandas_dtype(dtype._subtype_with_str)
10651065
# TODO copy=False is broken for astype_nansafe with int -> float, so cannot
10661066
# passthrough copy keyword: https://github.com/pandas-dev/pandas/issues/34456
10671067
sp_values = astype_nansafe(self.sp_values, subtype, copy=True)

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
is_timedelta64_dtype,
7171
is_timedelta64_ns_dtype,
7272
is_unsigned_integer_dtype,
73-
pandas_dtype,
7473
)
7574
from pandas.core.dtypes.dtypes import (
7675
DatetimeTZDtype,
@@ -962,7 +961,7 @@ def astype_nansafe(
962961
Parameters
963962
----------
964963
arr : ndarray
965-
dtype : np.dtype
964+
dtype : np.dtype or ExtensionDtype
966965
copy : bool, default True
967966
If False, a view will be attempted but may fail, if
968967
e.g. the item sizes don't align.
@@ -975,11 +974,11 @@ def astype_nansafe(
975974
The dtype was a datetime64/timedelta64 dtype, but it had no unit.
976975
"""
977976
# dispatch on extension dtype if needed
978-
if is_extension_array_dtype(dtype):
977+
if isinstance(dtype, ExtensionDtype):
979978
return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)
980979

981-
if not isinstance(dtype, np.dtype):
982-
dtype = pandas_dtype(dtype)
980+
elif not isinstance(dtype, np.dtype):
981+
raise ValueError("dtype must be np.dtype or ExtensionDtype")
983982

984983
if issubclass(dtype.type, str):
985984
return lib.ensure_string_array(

pandas/io/parsers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@ def _convert_to_ndarrays(
17141714
except (AttributeError, TypeError):
17151715
# invalid input to is_bool_dtype
17161716
pass
1717+
cast_type = pandas_dtype(cast_type)
17171718
cvals = self._cast_types(cvals, cast_type, c)
17181719

17191720
result[c] = cvals

pandas/tests/dtypes/test_common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,8 @@ def test__is_dtype_type(input_param, result):
716716
def test_astype_nansafe(val, typ):
717717
arr = np.array([val])
718718

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

743+
to_type = np.dtype(to_type)
744+
741745
with pytest.raises(TypeError, match="cannot astype"):
742746
astype_nansafe(arr, dtype=to_type)
743747

744748

745749
@pytest.mark.parametrize("from_type", [np.datetime64, np.timedelta64])
746750
def test_astype_object_preserves_datetime_na(from_type):
747751
arr = np.array([from_type("NaT")])
748-
result = astype_nansafe(arr, dtype="object")
752+
result = astype_nansafe(arr, dtype=np.dtype("object"))
749753

750754
assert isna(result)[0]
751755

0 commit comments

Comments
 (0)