From b3252ef4d6b5f652a7f6e2a0bbaf94fbd2b2c7bc Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 11 Jun 2021 16:11:21 -0700 Subject: [PATCH 1/2] REGR: Series[dt64/td64].astype(string) --- pandas/_libs/lib.pyx | 8 ++++++++ pandas/tests/frame/methods/test_astype.py | 17 +++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 0aec7e5e5a363..56a255afe6783 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -712,6 +712,14 @@ cpdef ndarray[object] ensure_string_array( Py_ssize_t i = 0, n = len(arr) if hasattr(arr, "to_numpy"): + + if hasattr(arr, "dtype") and arr.dtype.kind in ["m", "M"]: + # dtype check to exclude DataFrame + # GH#41409 TODO: not a great place for this + out = arr.astype(str) + out[arr.isna()] = na_value + return out + arr = arr.to_numpy() elif not isinstance(arr, np.ndarray): arr = np.array(arr, dtype="object") diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index 881f8db305240..0268f1896ea27 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -632,13 +632,9 @@ def test_astype_tz_object_conversion(self, tz): result = result.astype({"tz": "datetime64[ns, Europe/London]"}) tm.assert_frame_equal(result, expected) - def test_astype_dt64_to_string(self, frame_or_series, tz_naive_fixture, request): + def test_astype_dt64_to_string(self, frame_or_series, tz_naive_fixture): + # GH#41409 tz = tz_naive_fixture - if tz is None: - mark = pytest.mark.xfail( - reason="GH#36153 uses ndarray formatting instead of DTA formatting" - ) - request.node.add_marker(mark) dti = date_range("2016-01-01", periods=3, tz=tz) dta = dti._data @@ -660,6 +656,15 @@ def test_astype_dt64_to_string(self, frame_or_series, tz_naive_fixture, request) alt = obj.astype(str) assert np.all(alt.iloc[1:] == result.iloc[1:]) + def test_astype_td64_to_string(self, frame_or_series): + # GH#41409 + tdi = pd.timedelta_range("1 Day", periods=3) + obj = frame_or_series(tdi) + + expected = frame_or_series(["1 day", "2 days", "3 days"], dtype="string") + result = obj.astype("string") + tm.assert_equal(result, expected) + def test_astype_bytes(self): # GH#39474 result = DataFrame(["foo", "bar", "baz"]).astype(bytes) From 006956c657daccb6197742a4a788913508915e8e Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 11 Jun 2021 16:15:07 -0700 Subject: [PATCH 2/2] fix str->object --- pandas/_libs/lib.pyx | 2 +- pandas/tests/frame/methods/test_astype.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 56a255afe6783..67adf5540a3f9 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -716,7 +716,7 @@ cpdef ndarray[object] ensure_string_array( if hasattr(arr, "dtype") and arr.dtype.kind in ["m", "M"]: # dtype check to exclude DataFrame # GH#41409 TODO: not a great place for this - out = arr.astype(str) + out = arr.astype(str).astype(object) out[arr.isna()] = na_value return out diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index 0268f1896ea27..bd71f0a0716b4 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -661,7 +661,7 @@ def test_astype_td64_to_string(self, frame_or_series): tdi = pd.timedelta_range("1 Day", periods=3) obj = frame_or_series(tdi) - expected = frame_or_series(["1 day", "2 days", "3 days"], dtype="string") + expected = frame_or_series(["1 days", "2 days", "3 days"], dtype="string") result = obj.astype("string") tm.assert_equal(result, expected)