From 88335ae9e8306a17eecde31208a25c840e54711d Mon Sep 17 00:00:00 2001 From: Tomas Pavlik Date: Fri, 24 Feb 2023 10:59:12 +0100 Subject: [PATCH 1/2] BUG: Weighted rolling aggregations min_periods=0 --- pandas/core/window/rolling.py | 6 +++++- pandas/tests/window/test_win_type.py | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 97b3f23d65b44..46ce5950a1465 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1204,7 +1204,11 @@ def homogeneous_func(values: np.ndarray): def calc(x): additional_nans = np.array([np.nan] * offset) x = np.concatenate((x, additional_nans)) - return func(x, window, self.min_periods or len(window)) + return func( + x, + window, + self.min_periods if self.min_periods is not None else len(window), + ) with np.errstate(all="ignore"): # Our weighted aggregations return memoryviews diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index 8438e0727c174..a0d052257ec6b 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -321,16 +321,16 @@ def test_cmov_window_frame(f, xp, step): tm.assert_frame_equal(xp, rs) +@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4, 5]) @td.skip_if_no_scipy -def test_cmov_window_na_min_periods(step): - # min_periods +def test_cmov_window_na_min_periods(step, min_periods): vals = Series(np.random.randn(10)) vals[4] = np.nan vals[8] = np.nan - xp = vals.rolling(5, min_periods=4, center=True, step=step).mean() + xp = vals.rolling(5, min_periods=min_periods, center=True, step=step).mean() rs = vals.rolling( - 5, win_type="boxcar", min_periods=4, center=True, step=step + 5, win_type="boxcar", min_periods=min_periods, center=True, step=step ).mean() tm.assert_series_equal(xp, rs) From e1b53a614c5143ea86843ec32ad2a6b06e7c1667 Mon Sep 17 00:00:00 2001 From: Tomas Pavlik Date: Sun, 26 Feb 2023 12:10:24 +0100 Subject: [PATCH 2/2] added whatsnew --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index f6a6c81bfe25d..36ad235c5df0f 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -182,7 +182,7 @@ Plotting Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ -- +- Bug in weighted rolling aggregations when specifying ``min_periods=0`` (:issue:`51449`) - Reshaping