From eb4c5fbb3054d7252594eba6c7b862a054f08024 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 --- pandas/core/dtypes/cast.py | 2 +- pandas/tests/frame/test_apply.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 2a47a03b8d387d..45aa00d447210a 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 d12699397d1e45..48a141a657cbb3 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