Skip to content

Commit ff187c0

Browse files
BUG: Weighted rolling aggregations min_periods=0 (#51611)
* BUG: Weighted rolling aggregations min_periods=0 * added whatsnew --------- Co-authored-by: Tomas Pavlik <[email protected]>
1 parent 02ab370 commit ff187c0

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Plotting
189189
Groupby/resample/rolling
190190
^^^^^^^^^^^^^^^^^^^^^^^^
191191
- Bug in :meth:`DataFrameGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmax` return wrong dtype when used on empty DataFrameGroupBy or SeriesGroupBy (:issue:`51423`)
192+
- Bug in weighted rolling aggregations when specifying ``min_periods=0`` (:issue:`51449`)
192193
-
193194

194195
Reshaping

pandas/core/window/rolling.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,11 @@ def homogeneous_func(values: np.ndarray):
12041204
def calc(x):
12051205
additional_nans = np.array([np.nan] * offset)
12061206
x = np.concatenate((x, additional_nans))
1207-
return func(x, window, self.min_periods or len(window))
1207+
return func(
1208+
x,
1209+
window,
1210+
self.min_periods if self.min_periods is not None else len(window),
1211+
)
12081212

12091213
with np.errstate(all="ignore"):
12101214
# Our weighted aggregations return memoryviews

pandas/tests/window/test_win_type.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,16 @@ def test_cmov_window_frame(f, xp, step):
321321
tm.assert_frame_equal(xp, rs)
322322

323323

324+
@pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4, 5])
324325
@td.skip_if_no_scipy
325-
def test_cmov_window_na_min_periods(step):
326-
# min_periods
326+
def test_cmov_window_na_min_periods(step, min_periods):
327327
vals = Series(np.random.randn(10))
328328
vals[4] = np.nan
329329
vals[8] = np.nan
330330

331-
xp = vals.rolling(5, min_periods=4, center=True, step=step).mean()
331+
xp = vals.rolling(5, min_periods=min_periods, center=True, step=step).mean()
332332
rs = vals.rolling(
333-
5, win_type="boxcar", min_periods=4, center=True, step=step
333+
5, win_type="boxcar", min_periods=min_periods, center=True, step=step
334334
).mean()
335335
tm.assert_series_equal(xp, rs)
336336

0 commit comments

Comments
 (0)