Skip to content

Commit 63a8872

Browse files
committed
astype keeps nan when converting into string
1 parent 6813d77 commit 63a8872

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Indexing
145145
Missing
146146
^^^^^^^
147147

148-
-
148+
- When converting into string, :meth:`Series.astype` will keep ``np.nan`` as missing value (:issue:`25353`)
149149
-
150150

151151
MultiIndex

pandas/core/internals/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,12 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs):
621621
else:
622622
values = self.get_values(dtype=dtype)
623623

624+
skipna = kwargs.pop("skipna", True)
624625
# _astype_nansafe works fine with 1-d only
625626
vals1d = values.ravel()
626-
values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
627+
values = astype_nansafe(
628+
vals1d, dtype, copy=True, skipna=skipna, **kwargs
629+
)
627630

628631
# TODO(extension)
629632
# should we make this attribute?

pandas/tests/frame/test_dtypes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -627,13 +627,13 @@ def test_astype_str(self):
627627
assert_frame_equal(result, expected)
628628

629629
def test_astype_str_float(self):
630-
# see gh-11302
630+
# GH 25353
631631
result = DataFrame([np.NaN]).astype(str)
632-
expected = DataFrame(["nan"])
633-
632+
expected = DataFrame([np.nan], dtype=object)
634633
assert_frame_equal(result, expected)
635-
result = DataFrame([1.12345678901234567890]).astype(str)
636634

635+
# see gh-11302
636+
result = DataFrame([1.12345678901234567890]).astype(str)
637637
# < 1.14 truncates
638638
# >= 1.14 preserves the full repr
639639
val = "1.12345678901" if _np_version_under1p14 else "1.1234567890123457"

pandas/tests/reductions/test_reductions.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,10 @@ def test_mode_numerical_nan(self, dropna, expected):
11051105

11061106
@pytest.mark.parametrize(
11071107
"dropna, expected1, expected2, expected3",
1108-
[(True, ["b"], ["bar"], ["nan"]), (False, ["b"], [np.nan], ["nan"])],
1108+
[
1109+
(True, ["b"], ["bar"], Series(["bar"])),
1110+
(False, ["b"], [np.nan], Series([np.nan], dtype=object)),
1111+
],
11091112
)
11101113
def test_mode_str_obj(self, dropna, expected1, expected2, expected3):
11111114
# Test string and object types.
@@ -1127,7 +1130,6 @@ def test_mode_str_obj(self, dropna, expected1, expected2, expected3):
11271130

11281131
s = Series(data, dtype=object).astype(str)
11291132
result = s.mode(dropna)
1130-
expected3 = Series(expected3, dtype=str)
11311133
tm.assert_series_equal(result, expected3)
11321134

11331135
@pytest.mark.parametrize(

pandas/tests/series/test_dtypes.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_astype_datetime64tz(self):
147147
)
148148
def test_astype_str_map(self, dtype, series):
149149
# see gh-4405
150-
result = series.astype(dtype)
150+
result = series.astype(dtype, skipna=False)
151151
expected = series.map(str)
152152
tm.assert_series_equal(result, expected)
153153

@@ -171,6 +171,16 @@ def test_astype_str_cast(self):
171171
expected = Series([str("1 days 00:00:00.000000000")])
172172
tm.assert_series_equal(s, expected)
173173

174+
@pytest.mark.parametrize(
175+
"skipna, expected",
176+
[(True, Series(["1", "a", np.nan])), (False, Series(["1", "a", "nan"]))],
177+
)
178+
def test_astype_str(self, skipna, expected):
179+
# GH 25353
180+
ser = Series([1, "a", np.nan])
181+
result = ser.astype(str, skipna=skipna)
182+
tm.assert_series_equal(result, expected)
183+
174184
def test_astype_unicode(self):
175185
# see gh-7758: A bit of magic is required to set
176186
# default encoding to utf-8

0 commit comments

Comments
 (0)