From e64dee26d253b712ecc0c6e0c3adf4a2de1f8e62 Mon Sep 17 00:00:00 2001 From: Yuval Date: Mon, 7 Mar 2022 22:52:20 +0200 Subject: [PATCH 1/9] TST: added test for rolling with centering=True and axis=1 (#46135) --- pandas/tests/window/test_rolling.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 7e9bc121f06ff..2c4f8a6a934af 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1781,3 +1781,17 @@ def test_step_not_integer_raises(): def test_step_not_positive_raises(): with pytest.raises(ValueError, match="step must be >= 0"): DataFrame(range(2)).rolling(1, step=-1) + + +def test_rolling_center_axis_1(): + df = DataFrame({'a': [1, 1, 0, 0, 0, 1], + 'b': [1, 0, 0, 1, 0, 0], + 'c': [1, 0, 0, 1, 0, 1]}) + + result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() + + expected = DataFrame({'a': [None, None, None, None, None, None], + 'b': [3, 1, 0, 2, 0, 2], + 'c': [None, None, None, None, None, None]}) + + tm.assert_frame_equal(result, expected, check_dtype=False) From fef5f933d27511553b355496de82a2084f21cdbb Mon Sep 17 00:00:00 2001 From: Yuval Date: Mon, 7 Mar 2022 22:53:17 +0200 Subject: [PATCH 2/9] BUG: fixed bug of rolling with centering=True and axis=1 (#46135) --- pandas/core/window/rolling.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 7961b954d3a2a..370b593a3dc22 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1129,12 +1129,8 @@ def _center_window(self, result: np.ndarray, offset: int) -> np.ndarray: """ Center the result in the window for weighted rolling aggregations. """ - if self.axis > result.ndim - 1: - raise ValueError("Requested axis is larger then no. of argument dimensions") - if offset > 0: - lead_indexer = [slice(None)] * result.ndim - lead_indexer[self.axis] = slice(offset, None) + lead_indexer = [slice(offset, None)] result = np.copy(result[tuple(lead_indexer)]) return result From 2153f23733ee14ffde4bffb864a26090ed5c1642 Mon Sep 17 00:00:00 2001 From: Yuval Date: Tue, 8 Mar 2022 09:39:59 +0200 Subject: [PATCH 3/9] DOC: fixed bug of rolling with centering=True and axis=1 (#46135) --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 52c8b82ab47f9..8b586cfb9261c 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -306,6 +306,7 @@ Performance improvements Bug fixes ~~~~~~~~~ +- Fixed bug in window rolling when centering with axis=1 (:issue:`46135`) Categorical ^^^^^^^^^^^ From 86dc951e2980c954af1af81bb242b84bfccd25f7 Mon Sep 17 00:00:00 2001 From: Yuval Date: Tue, 8 Mar 2022 08:22:41 +0000 Subject: [PATCH 4/9] Fixes from pre-commit [automated commit] --- pandas/tests/window/test_rolling.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 2c4f8a6a934af..809893bacdb04 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1784,14 +1784,18 @@ def test_step_not_positive_raises(): def test_rolling_center_axis_1(): - df = DataFrame({'a': [1, 1, 0, 0, 0, 1], - 'b': [1, 0, 0, 1, 0, 0], - 'c': [1, 0, 0, 1, 0, 1]}) + df = DataFrame( + {"a": [1, 1, 0, 0, 0, 1], "b": [1, 0, 0, 1, 0, 0], "c": [1, 0, 0, 1, 0, 1]} + ) result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() - expected = DataFrame({'a': [None, None, None, None, None, None], - 'b': [3, 1, 0, 2, 0, 2], - 'c': [None, None, None, None, None, None]}) + expected = DataFrame( + { + "a": [None, None, None, None, None, None], + "b": [3, 1, 0, 2, 0, 2], + "c": [None, None, None, None, None, None], + } + ) tm.assert_frame_equal(result, expected, check_dtype=False) From e69b4f2bcc1eb7f61d1bb06340c0642888ec5240 Mon Sep 17 00:00:00 2001 From: Yuval Date: Tue, 22 Mar 2022 23:02:32 +0200 Subject: [PATCH 5/9] TST: adapted test_rolling_center_axis_1 to support check_dtype=True --- pandas/tests/window/test_rolling.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 2c4f8a6a934af..20d46251059cc 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1790,8 +1790,8 @@ def test_rolling_center_axis_1(): result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() - expected = DataFrame({'a': [None, None, None, None, None, None], - 'b': [3, 1, 0, 2, 0, 2], - 'c': [None, None, None, None, None, None]}) + expected = DataFrame({'a': [np.nan] * 6, + 'b': [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], + 'c': [np.nan] * 6}) - tm.assert_frame_equal(result, expected, check_dtype=False) + tm.assert_frame_equal(result, expected, check_dtype=True) From d627560b7acf0ba7c1ecd10ee1211cf61223f619 Mon Sep 17 00:00:00 2001 From: jyuv <45140931+jyuv@users.noreply.github.com> Date: Tue, 22 Mar 2022 22:01:26 +0000 Subject: [PATCH 6/9] Fixes from pre-commit [automated commit] --- pandas/tests/window/test_rolling.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 20d46251059cc..4edb1ce82b90d 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1784,14 +1784,14 @@ def test_step_not_positive_raises(): def test_rolling_center_axis_1(): - df = DataFrame({'a': [1, 1, 0, 0, 0, 1], - 'b': [1, 0, 0, 1, 0, 0], - 'c': [1, 0, 0, 1, 0, 1]}) + df = DataFrame( + {"a": [1, 1, 0, 0, 0, 1], "b": [1, 0, 0, 1, 0, 0], "c": [1, 0, 0, 1, 0, 1]} + ) result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() - expected = DataFrame({'a': [np.nan] * 6, - 'b': [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], - 'c': [np.nan] * 6}) + expected = DataFrame( + {"a": [np.nan] * 6, "b": [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], "c": [np.nan] * 6} + ) tm.assert_frame_equal(result, expected, check_dtype=True) From a87748ed6ea45c10a5c44f15026a042e9c20ef85 Mon Sep 17 00:00:00 2001 From: Yuval Date: Wed, 23 Mar 2022 11:46:43 +0200 Subject: [PATCH 7/9] TST: move test_rolling_center_axis_1 from test_rolling.py to test_win_type.py --- pandas/tests/window/test_rolling.py | 14 -------------- pandas/tests/window/test_win_type.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 20d46251059cc..7e9bc121f06ff 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1781,17 +1781,3 @@ def test_step_not_integer_raises(): def test_step_not_positive_raises(): with pytest.raises(ValueError, match="step must be >= 0"): DataFrame(range(2)).rolling(1, step=-1) - - -def test_rolling_center_axis_1(): - df = DataFrame({'a': [1, 1, 0, 0, 0, 1], - 'b': [1, 0, 0, 1, 0, 0], - 'c': [1, 0, 0, 1, 0, 1]}) - - result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() - - expected = DataFrame({'a': [np.nan] * 6, - 'b': [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], - 'c': [np.nan] * 6}) - - tm.assert_frame_equal(result, expected, check_dtype=True) diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index c356c9bdc7742..b05892caf58b8 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -676,3 +676,18 @@ def test_cmov_window_special_linear_range(win_types_special, step): .mean(**kwds[win_types_special]) ) tm.assert_series_equal(xp, rs) + + +@td.skip_if_no_scipy +def test_rolling_center_axis_1(): + df = DataFrame({'a': [1, 1, 0, 0, 0, 1], + 'b': [1, 0, 0, 1, 0, 0], + 'c': [1, 0, 0, 1, 0, 1]}) + + result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() + + expected = DataFrame({'a': [np.nan] * 6, + 'b': [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], + 'c': [np.nan] * 6}) + + tm.assert_frame_equal(result, expected, check_dtype=True) From 63e116e9124e3d341375fe43d87232d2a5674400 Mon Sep 17 00:00:00 2001 From: jyuv <45140931+jyuv@users.noreply.github.com> Date: Wed, 23 Mar 2022 09:58:11 +0000 Subject: [PATCH 8/9] Fixes from pre-commit [automated commit] --- pandas/tests/window/test_win_type.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index b05892caf58b8..3ce9e9024144c 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -680,14 +680,14 @@ def test_cmov_window_special_linear_range(win_types_special, step): @td.skip_if_no_scipy def test_rolling_center_axis_1(): - df = DataFrame({'a': [1, 1, 0, 0, 0, 1], - 'b': [1, 0, 0, 1, 0, 0], - 'c': [1, 0, 0, 1, 0, 1]}) + df = DataFrame( + {"a": [1, 1, 0, 0, 0, 1], "b": [1, 0, 0, 1, 0, 0], "c": [1, 0, 0, 1, 0, 1]} + ) result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() - expected = DataFrame({'a': [np.nan] * 6, - 'b': [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], - 'c': [np.nan] * 6}) + expected = DataFrame( + {"a": [np.nan] * 6, "b": [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], "c": [np.nan] * 6} + ) tm.assert_frame_equal(result, expected, check_dtype=True) From 0d7f3757be20d0c23c15b9850a6a090f66eac58d Mon Sep 17 00:00:00 2001 From: Yuval Date: Mon, 2 May 2022 13:24:21 +0300 Subject: [PATCH 9/9] DOC: moved bug mention to rolling doc section and specify conditions (#46135) --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 56dc943516d0e..27d78d7c313c4 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -474,7 +474,6 @@ Performance improvements Bug fixes ~~~~~~~~~ -- Fixed bug in window rolling when centering with axis=1 (:issue:`46135`) Categorical ^^^^^^^^^^^ @@ -630,6 +629,7 @@ Groupby/resample/rolling - Bug in :meth:`Rolling.var` and :meth:`Rolling.std` would give non-zero result with window of same values (:issue:`42064`) - Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`) - Bug in :meth:`Grouper.__repr__` where ``dropna`` was not included. Now it is (:issue:`46754`) +- Bug in :meth:`DataFrame.rolling` gives ValueError when center=True, axis=1 and win_type is specified (:issue:`46135`) Reshaping ^^^^^^^^^