Skip to content

Commit 45d0898

Browse files
authored
CLN: Don't use astype_nansafe after to_numpy (#34110)
1 parent a94b13a commit 45d0898

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

pandas/core/arrays/boolean.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pandas.compat import set_function_name
1010
from pandas.compat.numpy import function as nv
1111

12-
from pandas.core.dtypes.cast import astype_nansafe
1312
from pandas.core.dtypes.common import (
1413
is_bool_dtype,
1514
is_extension_array_dtype,
@@ -399,8 +398,7 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
399398
if is_float_dtype(dtype):
400399
na_value = np.nan
401400
# coerce
402-
data = self.to_numpy(na_value=na_value)
403-
return astype_nansafe(data, dtype, copy=False)
401+
return self.to_numpy(dtype=dtype, na_value=na_value, copy=False)
404402

405403
def _values_for_argsort(self) -> np.ndarray:
406404
"""

pandas/core/arrays/integer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pandas.compat.numpy import function as nv
1111
from pandas.util._decorators import cache_readonly
1212

13-
from pandas.core.dtypes.cast import astype_nansafe
1413
from pandas.core.dtypes.common import (
1514
is_bool_dtype,
1615
is_datetime64_dtype,
@@ -460,14 +459,13 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
460459
# coerce
461460
if is_float_dtype(dtype):
462461
# In astype, we consider dtype=float to also mean na_value=np.nan
463-
kwargs = dict(na_value=np.nan)
462+
na_value = np.nan
464463
elif is_datetime64_dtype(dtype):
465-
kwargs = dict(na_value=np.datetime64("NaT"))
464+
na_value = np.datetime64("NaT")
466465
else:
467-
kwargs = {}
466+
na_value = lib.no_default
468467

469-
data = self.to_numpy(dtype=dtype, **kwargs)
470-
return astype_nansafe(data, dtype, copy=False)
468+
return self.to_numpy(dtype=dtype, na_value=na_value, copy=False)
471469

472470
def _values_for_argsort(self) -> np.ndarray:
473471
"""

pandas/tests/arrays/boolean/test_astype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_astype():
2020
tm.assert_numpy_array_equal(result, expected)
2121

2222
result = arr.astype("str")
23-
expected = np.array(["True", "False", "<NA>"], dtype="object")
23+
expected = np.array(["True", "False", "<NA>"], dtype="<U5")
2424
tm.assert_numpy_array_equal(result, expected)
2525

2626
# no missing values

pandas/tests/arrays/integer/test_dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def test_to_numpy_na_raises(dtype):
237237

238238
def test_astype_str():
239239
a = pd.array([1, 2, None], dtype="Int64")
240-
expected = np.array(["1", "2", "<NA>"], dtype=object)
240+
expected = np.array(["1", "2", "<NA>"], dtype="<U21")
241241

242242
tm.assert_numpy_array_equal(a.astype(str), expected)
243243
tm.assert_numpy_array_equal(a.astype("str"), expected)

0 commit comments

Comments
 (0)