Skip to content
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

CLN: Don't use astype_nansafe after to_numpy #34110

Merged
merged 2 commits into from
Jun 1, 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
4 changes: 1 addition & 3 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv

from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.common import (
is_bool_dtype,
is_extension_array_dtype,
Expand Down Expand Up @@ -399,8 +398,7 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
if is_float_dtype(dtype):
na_value = np.nan
# coerce
data = self.to_numpy(na_value=na_value)
return astype_nansafe(data, dtype, copy=False)
return self.to_numpy(dtype=dtype, na_value=na_value, copy=False)

def _values_for_argsort(self) -> np.ndarray:
"""
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.common import (
is_bool_dtype,
is_datetime64_dtype,
Expand Down Expand Up @@ -457,14 +456,13 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
# coerce
if is_float_dtype(dtype):
# In astype, we consider dtype=float to also mean na_value=np.nan
kwargs = dict(na_value=np.nan)
na_value = np.nan
elif is_datetime64_dtype(dtype):
kwargs = dict(na_value=np.datetime64("NaT"))
na_value = np.datetime64("NaT")
else:
kwargs = {}
na_value = lib.no_default

data = self.to_numpy(dtype=dtype, **kwargs)
return astype_nansafe(data, dtype, copy=False)
return self.to_numpy(dtype=dtype, na_value=na_value, copy=False)

def _values_for_argsort(self) -> np.ndarray:
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/boolean/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_astype():
tm.assert_numpy_array_equal(result, expected)

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

# no missing values
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/integer/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def test_to_numpy_na_raises(dtype):

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

tm.assert_numpy_array_equal(a.astype(str), expected)
tm.assert_numpy_array_equal(a.astype("str"), expected)
Expand Down