From de593d364cdc117f825c1c8517cc86de4aa8f214 Mon Sep 17 00:00:00 2001 From: Kernc Date: Wed, 4 Mar 2020 00:32:53 +0100 Subject: [PATCH 1/4] BUG: Fix DataFrame.apply(..., raw=True) not calling with raw array --- pandas/core/apply.py | 2 +- pandas/tests/frame/test_apply.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 70e0a129c055f..ceb45bc71326e 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -179,7 +179,7 @@ def get_result(self): return self.apply_empty_result() # raw - elif self.raw and not self.obj._is_mixed_type: + elif self.raw: return self.apply_raw() return self.apply_standard() diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index fe6abef97acc4..ad029327fd926 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -235,7 +235,7 @@ def test_apply_broadcast_error(self, int_frame_const_col): with pytest.raises(ValueError): df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast") - def test_apply_raw(self, float_frame): + def test_apply_raw(self, float_frame, mixed_type_frame): result0 = float_frame.apply(np.mean, raw=True) result1 = float_frame.apply(np.mean, axis=1, raw=True) @@ -250,6 +250,13 @@ def test_apply_raw(self, float_frame): expected = float_frame * 2 tm.assert_frame_equal(result, expected) + # Mixed dtype + def _assert_raw(x): + assert isinstance(x, np.ndarray) + + mixed_type_frame.apply(_assert_raw, raw=True) + mixed_type_frame.apply(_assert_raw, axis=1, raw=True) + def test_apply_axis1(self, float_frame): d = float_frame.index[0] tapplied = float_frame.apply(np.mean, axis=1) From a4bfe80f1cb7f2f6b2015825f3e7904e7deaa449 Mon Sep 17 00:00:00 2001 From: Kernc Date: Wed, 4 Mar 2020 17:59:33 +0100 Subject: [PATCH 2/4] assert raw also for single dtype; assert ndim == 1 --- pandas/tests/frame/test_apply.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index ad029327fd926..e9c81fd91b8e9 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -236,6 +236,14 @@ def test_apply_broadcast_error(self, int_frame_const_col): df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast") def test_apply_raw(self, float_frame, mixed_type_frame): + + def _assert_raw(x): + assert isinstance(x, np.ndarray) + assert x.ndim == 1 + + float_frame.apply(_assert_raw, raw=True) + float_frame.apply(_assert_raw, axis=1, raw=True) + result0 = float_frame.apply(np.mean, raw=True) result1 = float_frame.apply(np.mean, axis=1, raw=True) @@ -250,10 +258,7 @@ def test_apply_raw(self, float_frame, mixed_type_frame): expected = float_frame * 2 tm.assert_frame_equal(result, expected) - # Mixed dtype - def _assert_raw(x): - assert isinstance(x, np.ndarray) - + # Mixed dtype (GH-32423) mixed_type_frame.apply(_assert_raw, raw=True) mixed_type_frame.apply(_assert_raw, axis=1, raw=True) From fb1d4c1e6cba43b4303b81d381b48bd989e980c1 Mon Sep 17 00:00:00 2001 From: Kernc Date: Wed, 4 Mar 2020 18:00:43 +0100 Subject: [PATCH 3/4] Add whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 57c53f73962dc..618b244dede4d 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -328,6 +328,7 @@ Reshaping - :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`) - Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`) - :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`) +- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`) Sparse From e66cd23b1a073efbbcb1f2f91d1f914f0b9b2b50 Mon Sep 17 00:00:00 2001 From: Kernc Date: Wed, 4 Mar 2020 18:04:05 +0100 Subject: [PATCH 4/4] black disagreed --- pandas/tests/frame/test_apply.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index e9c81fd91b8e9..c117f7ce7ac60 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -236,7 +236,6 @@ def test_apply_broadcast_error(self, int_frame_const_col): df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast") def test_apply_raw(self, float_frame, mixed_type_frame): - def _assert_raw(x): assert isinstance(x, np.ndarray) assert x.ndim == 1