From 12e4d398cf9013794d24cf0fbd68da55464162ae Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 24 Sep 2020 20:09:55 +0100 Subject: [PATCH 1/3] REGR: DataFrame.apply() with raw option and func returning string --- pandas/core/apply.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index bbf832f33065b..592824557e771 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -216,7 +216,21 @@ def apply_empty_result(self): def apply_raw(self): """ apply to the values as a numpy array """ - result = np.apply_along_axis(self.f, self.axis, self.values) + + def wrap_function(func): + """ + Wrap user supplied function to work around numpy issue. + + see https://github.com/numpy/numpy/issues/8352 + """ + def wrapper(*args, **kwargs): + result = func(*args, **kwargs) + if isinstance(result, str): + result = np.array(result, dtype=object) + return result + return wrapper + + result = np.apply_along_axis(wrap_function(self.f), self.axis, self.values) # TODO: mixed type case if result.ndim == 2: From 3526b151f7d18da7870a22cdcbd0c9af2660d736 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 24 Sep 2020 20:34:39 +0100 Subject: [PATCH 2/3] add test --- pandas/core/apply.py | 2 ++ pandas/tests/frame/apply/test_frame_apply.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 592824557e771..002e260742dc5 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -223,11 +223,13 @@ def wrap_function(func): see https://github.com/numpy/numpy/issues/8352 """ + def wrapper(*args, **kwargs): result = func(*args, **kwargs) if isinstance(result, str): result = np.array(result, dtype=object) return result + return wrapper result = np.apply_along_axis(wrap_function(self.f), self.axis, self.values) diff --git a/pandas/tests/frame/apply/test_frame_apply.py b/pandas/tests/frame/apply/test_frame_apply.py index e25b681c8c7c3..3f859bb4ee39e 100644 --- a/pandas/tests/frame/apply/test_frame_apply.py +++ b/pandas/tests/frame/apply/test_frame_apply.py @@ -1545,3 +1545,11 @@ def test_apply_no_suffix_index(): ) tm.assert_frame_equal(result, expected) + + +def test_apply_raw_returns_string(): + # https://github.com/pandas-dev/pandas/issues/35940 + df = pd.DataFrame({"A": ["aa", "bbb"]}) + result = df.apply(lambda x: x[0], axis=1, raw=True) + expected = pd.Series(["aa", "bbb"]) + tm.assert_series_equal(result, expected) From 51bb7900ee39da44fc4af7e4628862cbcd6b7d9a Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 24 Sep 2020 20:37:22 +0100 Subject: [PATCH 3/3] release note --- doc/source/whatsnew/v1.1.3.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.3.rst b/doc/source/whatsnew/v1.1.3.rst index c1effad34ab93..34595ea4ec50f 100644 --- a/doc/source/whatsnew/v1.1.3.rst +++ b/doc/source/whatsnew/v1.1.3.rst @@ -35,6 +35,7 @@ Fixed regressions - Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`) - Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`) - Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`, :issue:`36377`) +- Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`) - Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`) - Fixed regression in :class:`Period` incorrect value for ordinal over the maximum timestamp (:issue:`36430`)