7
7
import inspect
8
8
from typing import (
9
9
TYPE_CHECKING ,
10
- cast ,
11
10
overload ,
12
11
)
13
12
34
33
ExtensionDtype ,
35
34
PandasDtype ,
36
35
)
37
- from pandas .core .dtypes .missing import isna
38
36
39
37
if TYPE_CHECKING :
40
- from pandas .core .arrays import (
41
- DatetimeArray ,
42
- ExtensionArray ,
43
- TimedeltaArray ,
44
- )
38
+ from pandas .core .arrays import ExtensionArray
45
39
46
40
47
41
_dtype_obj = np .dtype (object )
48
42
49
43
50
44
@overload
51
- def astype_nansafe (
45
+ def _astype_nansafe (
52
46
arr : np .ndarray , dtype : np .dtype , copy : bool = ..., skipna : bool = ...
53
47
) -> np .ndarray :
54
48
...
55
49
56
50
57
51
@overload
58
- def astype_nansafe (
52
+ def _astype_nansafe (
59
53
arr : np .ndarray , dtype : ExtensionDtype , copy : bool = ..., skipna : bool = ...
60
54
) -> ExtensionArray :
61
55
...
62
56
63
57
64
- def astype_nansafe (
58
+ def _astype_nansafe (
65
59
arr : np .ndarray , dtype : DtypeObj , copy : bool = True , skipna : bool = False
66
60
) -> ArrayLike :
67
61
"""
@@ -90,13 +84,12 @@ def astype_nansafe(
90
84
elif not isinstance (dtype , np .dtype ): # pragma: no cover
91
85
raise ValueError ("dtype must be np.dtype or ExtensionDtype" )
92
86
93
- if arr .dtype .kind in ["m" , "M" ] and (
94
- issubclass (dtype .type , str ) or dtype == _dtype_obj
95
- ):
87
+ if arr .dtype .kind in ["m" , "M" ]:
96
88
from pandas .core .construction import ensure_wrapped_if_datetimelike
97
89
98
90
arr = ensure_wrapped_if_datetimelike (arr )
99
- return arr .astype (dtype , copy = copy )
91
+ res = arr .astype (dtype , copy = copy )
92
+ return np .asarray (res )
100
93
101
94
if issubclass (dtype .type , str ):
102
95
shape = arr .shape
@@ -106,39 +99,6 @@ def astype_nansafe(
106
99
arr , skipna = skipna , convert_na_value = False
107
100
).reshape (shape )
108
101
109
- elif is_datetime64_dtype (arr .dtype ):
110
- if dtype == np .int64 :
111
- if isna (arr ).any ():
112
- raise ValueError ("Cannot convert NaT values to integer" )
113
- return arr .view (dtype )
114
-
115
- # allow frequency conversions
116
- if dtype .kind == "M" :
117
- from pandas .core .construction import ensure_wrapped_if_datetimelike
118
-
119
- dta = ensure_wrapped_if_datetimelike (arr )
120
- dta = cast ("DatetimeArray" , dta )
121
- return dta .astype (dtype , copy = copy )._ndarray
122
-
123
- raise TypeError (f"cannot astype a datetimelike from [{ arr .dtype } ] to [{ dtype } ]" )
124
-
125
- elif is_timedelta64_dtype (arr .dtype ):
126
- if dtype == np .int64 :
127
- if isna (arr ).any ():
128
- raise ValueError ("Cannot convert NaT values to integer" )
129
- return arr .view (dtype )
130
-
131
- elif dtype .kind == "m" :
132
- # give the requested dtype for supported units (s, ms, us, ns)
133
- # and doing the old convert-to-float behavior otherwise.
134
- from pandas .core .construction import ensure_wrapped_if_datetimelike
135
-
136
- tda = ensure_wrapped_if_datetimelike (arr )
137
- tda = cast ("TimedeltaArray" , tda )
138
- return tda .astype (dtype , copy = copy )._ndarray
139
-
140
- raise TypeError (f"cannot astype a timedelta from [{ arr .dtype } ] to [{ dtype } ]" )
141
-
142
102
elif np .issubdtype (arr .dtype , np .floating ) and is_integer_dtype (dtype ):
143
103
return _astype_float_to_int_nansafe (arr , dtype , copy )
144
104
@@ -231,7 +191,7 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
231
191
values = values .astype (dtype , copy = copy )
232
192
233
193
else :
234
- values = astype_nansafe (values , dtype , copy = copy )
194
+ values = _astype_nansafe (values , dtype , copy = copy )
235
195
236
196
# in pandas we don't store numpy str dtypes, so convert to object
237
197
if isinstance (dtype , np .dtype ) and issubclass (values .dtype .type , str ):
@@ -288,7 +248,7 @@ def astype_array_safe(
288
248
try :
289
249
new_values = astype_array (values , dtype , copy = copy )
290
250
except (ValueError , TypeError ):
291
- # e.g. astype_nansafe can fail on object-dtype of strings
251
+ # e.g. _astype_nansafe can fail on object-dtype of strings
292
252
# trying to convert to float
293
253
if errors == "ignore" :
294
254
new_values = values
0 commit comments