Skip to content

Skip converts na values to strings #45971

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

Closed
Closed
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/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
values = values.astype(dtype, copy=copy)

else:
values = astype_nansafe(values, dtype, copy=copy)
values = astype_nansafe(values, dtype, copy=copy, skipna=True)

# in pandas we don't store numpy str dtypes, so convert to object
if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,15 @@ def test_compare_complex_dtypes():

with pytest.raises(TypeError, match=msg):
df.lt(df.astype(object))


@pytest.mark.parametrize(
"array",
[(["foo", "bar", pd.NA]), (["foo", "bar", np.NaN]), (["foo", "bar", None])],
)
def test_skip_convert_nan_values_to_strings(array):
# GH 44156
result = Series(array).astype("str")
expected = Series(array)

tm.assert_series_equal(result, expected)