From cf649d0f0bbfba11a02be4dee1f75f520e669f98 Mon Sep 17 00:00:00 2001 From: Veronur Date: Mon, 15 Jun 2020 20:39:49 -0300 Subject: [PATCH] GH34529 correction and test for issue-#34529 made the formating changes fixing tests on issue-#34529 add whats new entry on issue-#34539 add whats new entry correction issue-#34539 whats new correction issue-#34539 --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/core/dtypes/cast.py | 2 +- pandas/tests/frame/test_apply.py | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 2243790a663df..d3972743be175 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1012,6 +1012,7 @@ Reshaping - Ensure only named functions can be used in :func:`eval()` (:issue:`32460`) - Bug in :func:`Dataframe.aggregate` and :func:`Series.aggregate` was causing recursive loop in some cases (:issue:`34224`) - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) +- Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`) Sparse ^^^^^^ diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 2a47a03b8d387..45aa00d447210 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1608,7 +1608,7 @@ def construct_1d_ndarray_preserving_na( """ subarr = np.array(values, dtype=dtype, copy=copy) - if dtype is not None and dtype.kind in ("U", "S"): + if dtype is not None and dtype.kind == "U": # GH-21083 # We can't just return np.array(subarr, dtype='str') since # NumPy will convert the non-string objects into strings diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index d12699397d1e4..48a141a657cbb 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -785,6 +785,17 @@ def non_reducing_function(val): df.applymap(func) assert values == df.a.to_list() + def test_apply_with_byte_string(self): + # GH 34529 + df = pd.DataFrame(np.array([b"abcd", b"efgh"]), columns=["col"]) + expected = pd.DataFrame( + np.array([b"abcd", b"efgh"]), columns=["col"], dtype=object + ) + # After we make the aply we exect a dataframe just + # like the original but with the object datatype + result = df.apply(lambda x: x.astype("object")) + tm.assert_frame_equal(result, expected) + class TestInferOutputShape: # the user has supplied an opaque UDF where