Skip to content

Commit 44dcef4

Browse files
jyuvYuval
and
Yuval
authored
BUG: fix rolling with centering and axis=1 (#46135) (#46265)
* TST: added test for rolling with centering=True and axis=1 (#46135) * BUG: fixed bug of rolling with centering=True and axis=1 (#46135) * DOC: fixed bug of rolling with centering=True and axis=1 (#46135) * Fixes from pre-commit [automated commit] * TST: adapted test_rolling_center_axis_1 to support check_dtype=True * Fixes from pre-commit [automated commit] * TST: move test_rolling_center_axis_1 from test_rolling.py to test_win_type.py * Fixes from pre-commit [automated commit] * DOC: moved bug mention to rolling doc section and specify conditions (#46135) Co-authored-by: Yuval <yuval,[email protected]>
1 parent 78ee555 commit 44dcef4

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ Groupby/resample/rolling
708708
- Bug in :meth:`Rolling.var` and :meth:`Rolling.std` would give non-zero result with window of same values (:issue:`42064`)
709709
- Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`)
710710
- Bug in :meth:`Grouper.__repr__` where ``dropna`` was not included. Now it is (:issue:`46754`)
711+
- Bug in :meth:`DataFrame.rolling` gives ValueError when center=True, axis=1 and win_type is specified (:issue:`46135`)
711712

712713
Reshaping
713714
^^^^^^^^^

pandas/core/window/rolling.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,12 +1129,8 @@ def _center_window(self, result: np.ndarray, offset: int) -> np.ndarray:
11291129
"""
11301130
Center the result in the window for weighted rolling aggregations.
11311131
"""
1132-
if self.axis > result.ndim - 1:
1133-
raise ValueError("Requested axis is larger then no. of argument dimensions")
1134-
11351132
if offset > 0:
1136-
lead_indexer = [slice(None)] * result.ndim
1137-
lead_indexer[self.axis] = slice(offset, None)
1133+
lead_indexer = [slice(offset, None)]
11381134
result = np.copy(result[tuple(lead_indexer)])
11391135
return result
11401136

pandas/tests/window/test_win_type.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,18 @@ def test_weighted_var_big_window_no_segfault(win_types, center):
686686
expected = Series(np.NaN)
687687

688688
tm.assert_series_equal(result, expected)
689+
690+
691+
@td.skip_if_no_scipy
692+
def test_rolling_center_axis_1():
693+
df = DataFrame(
694+
{"a": [1, 1, 0, 0, 0, 1], "b": [1, 0, 0, 1, 0, 0], "c": [1, 0, 0, 1, 0, 1]}
695+
)
696+
697+
result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum()
698+
699+
expected = DataFrame(
700+
{"a": [np.nan] * 6, "b": [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], "c": [np.nan] * 6}
701+
)
702+
703+
tm.assert_frame_equal(result, expected, check_dtype=True)

0 commit comments

Comments
 (0)