From b47f9274343c9ba4f91ffd227b3a0f3d545ade61 Mon Sep 17 00:00:00 2001 From: Daniel Saxton <2658661+dsaxton@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:24:58 -0500 Subject: [PATCH 1/2] Backport PR #37034: REGR: Fix casting of None to str during astype --- doc/source/whatsnew/v1.1.4.rst | 1 + pandas/core/dtypes/cast.py | 4 +++- pandas/tests/series/methods/test_astype.py | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.1.4.rst b/doc/source/whatsnew/v1.1.4.rst index 3ad8d981be2c9..ca58239dccb8d 100644 --- a/doc/source/whatsnew/v1.1.4.rst +++ b/doc/source/whatsnew/v1.1.4.rst @@ -17,6 +17,7 @@ Fixed regressions - Fixed regression where attempting to mutate a :class:`DateOffset` object would no longer raise an ``AttributeError`` (:issue:`36940`) - Fixed regression where :meth:`DataFrame.agg` would fail with :exc:`TypeError` when passed positional arguments to be passed on to the aggregation function (:issue:`36948`). - Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`) +- Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index a87bddef481b5..bdf294a380edc 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -922,7 +922,9 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False): dtype = pandas_dtype(dtype) if issubclass(dtype.type, str): - return lib.ensure_string_array(arr.ravel(), skipna=skipna).reshape(arr.shape) + return lib.ensure_string_array( + arr.ravel(), skipna=skipna, convert_na_value=False + ).reshape(arr.shape) elif is_datetime64_dtype(arr): if is_object_dtype(dtype): diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 7449d8d65ef96..eea839c380f0b 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas import Interval, Series, Timestamp, date_range +from pandas import NA, Interval, Series, Timestamp, date_range import pandas._testing as tm @@ -55,3 +55,18 @@ def test_astype_from_float_to_str(self, dtype): result = s.astype(str) expected = Series(["0.1"]) tm.assert_series_equal(result, expected) + + @pytest.mark.parametrize( + "value, string_value", + [ + (None, "None"), + (np.nan, "nan"), + (NA, ""), + ], + ) + def test_astype_to_str_preserves_na(self, value, string_value): + # https://github.com/pandas-dev/pandas/issues/36904 + s = Series(["a", "b", value], dtype=object) + result = s.astype(str) + expected = Series(["a", "b", string_value], dtype=object) + tm.assert_series_equal(result, expected) From 835ac88cc1dd96367cfe3ff211fb8ec2b6d84206 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 12 Oct 2020 15:55:45 +0100 Subject: [PATCH 2/2] black-19.10b0 --- pandas/tests/series/methods/test_astype.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index eea839c380f0b..ad960d4c65268 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -57,12 +57,7 @@ def test_astype_from_float_to_str(self, dtype): tm.assert_series_equal(result, expected) @pytest.mark.parametrize( - "value, string_value", - [ - (None, "None"), - (np.nan, "nan"), - (NA, ""), - ], + "value, string_value", [(None, "None"), (np.nan, "nan"), (NA, "")], ) def test_astype_to_str_preserves_na(self, value, string_value): # https://github.com/pandas-dev/pandas/issues/36904