From 2a2e9f38e3a5609bc13250f1409bf17c94933d81 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Thu, 10 Feb 2022 11:35:26 -0800 Subject: [PATCH 1/2] BUG: rolling(axis=1).apply() raising ValueError --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/window/rolling.py | 3 ++- pandas/tests/window/test_apply.py | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 24d6e3cece296..68c5b8e41e811 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -361,6 +361,7 @@ Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug in :meth:`DataFrame.resample` ignoring ``closed="right"`` on :class:`TimedeltaIndex` (:issue:`45414`) - Bug in :meth:`.DataFrameGroupBy.transform` fails when the input DataFrame has multiple columns (:issue:`27469`) +- Bug in :meth:`~Rolling.apply` with ``axis=1`` raising an erroneous ``ValueError`` (:issue:`45912`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index bbd0181e47401..72ebbcbc65e5e 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1343,7 +1343,8 @@ def _generate_cython_apply_func( def apply_func(values, begin, end, min_periods, raw=raw): if not raw: - values = Series(values, index=self.obj.index) + # GH 45912 + values = Series(values, index=self._on) return window_func(values, begin, end, min_periods) return apply_func diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py index 8e690a677aa98..6683137960146 100644 --- a/pandas/tests/window/test_apply.py +++ b/pandas/tests/window/test_apply.py @@ -308,3 +308,11 @@ def test_center_reindex_frame(raw, frame): ) frame_rs = frame.rolling(window=25, min_periods=minp, center=True).apply(f, raw=raw) tm.assert_frame_equal(frame_xp, frame_rs) + + +def test_axis1(raw): + # GH 45912 + df = DataFrame([1, 2]) + result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw) + expected = DataFrame([1.0, 2.0]) + tm.assert_frame_equal(result, expected) From 80b60d7ccaf4ec7f96e72513012dd3aaf30a8207 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Thu, 10 Feb 2022 17:00:32 -0800 Subject: [PATCH 2/2] Move to 1.4.1 --- doc/source/whatsnew/v1.4.1.rst | 1 + doc/source/whatsnew/v1.5.0.rst | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.1.rst b/doc/source/whatsnew/v1.4.1.rst index 19d8a2a9302d7..4536c06eb0867 100644 --- a/doc/source/whatsnew/v1.4.1.rst +++ b/doc/source/whatsnew/v1.4.1.rst @@ -36,6 +36,7 @@ Bug fixes - Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`) - Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`) - Fixed builtin highlighters in :class:`.Styler` to be responsive to ``NA`` with nullable dtypes (:issue:`45804`) +- Bug in :meth:`~Rolling.apply` with ``axis=1`` raising an erroneous ``ValueError`` (:issue:`45912`) .. --------------------------------------------------------------------------- diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 889f8ed82985a..10802850714df 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -365,7 +365,6 @@ Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug in :meth:`DataFrame.resample` ignoring ``closed="right"`` on :class:`TimedeltaIndex` (:issue:`45414`) - Bug in :meth:`.DataFrameGroupBy.transform` fails when the input DataFrame has multiple columns (:issue:`27469`) -- Bug in :meth:`~Rolling.apply` with ``axis=1`` raising an erroneous ``ValueError`` (:issue:`45912`) Reshaping ^^^^^^^^^