Skip to content

Commit 97adfd9

Browse files
committed
fix roll_generic in window.pyx
add new test method for min_periods in test_window::TestRolling
1 parent 3882c39 commit 97adfd9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

pandas/_libs/window.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1428,14 +1428,15 @@ def roll_generic(ndarray[float64_t, cast=True] input,
14281428
if n == 0:
14291429
return input
14301430

1431-
counts = roll_sum(np.concatenate([np.isfinite(input).astype(float),
1432-
np.array([0.] * offset)]),
1433-
win, minp, index, closed)[offset:]
1434-
14351431
start, end, N, win, minp, is_variable = get_window_indexer(input, win,
14361432
minp, index,
14371433
closed,
14381434
floor=0)
1435+
1436+
counts = roll_sum(np.concatenate([np.isfinite(input).astype(float),
1437+
np.array([0.] * offset)]),
1438+
win, minp, index, closed)[offset:]
1439+
14391440
output = np.empty(N, dtype=float)
14401441

14411442

pandas/tests/test_window.py

+20
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,26 @@ def test_constructor_with_timedelta_window(self):
423423
expected = df.rolling('3D').sum()
424424
tm.assert_frame_equal(result, expected)
425425

426+
def test_constructor_with_timedelta_window_and_minperiods(self):
427+
# GH 15440
428+
n = 10
429+
df = pd.DataFrame({'value': np.arange(n)},
430+
index=pd.date_range('2015-12-24',
431+
periods=n,
432+
freq="D"))
433+
expected_data = np.append([np.NaN, 1.], np.arange(3., 27., 3))
434+
for window in [timedelta(days=3), pd.Timedelta(days=3)]:
435+
result_roll_sum = df.rolling(window=window, min_periods=2).sum()
436+
result_roll_generic = df.rolling(window=window, min_periods=2).apply(sum)
437+
expected = pd.DataFrame({'value': expected_data},
438+
index=pd.date_range('2015-12-24',
439+
periods=n,
440+
freq="D"))
441+
tm.assert_frame_equal(result_roll_sum, expected)
442+
tm.assert_frame_equal(result_roll_generic, expected)
443+
expected = df.rolling('3D', min_periods=2).sum()
444+
tm.assert_frame_equal(result_roll_generic, expected)
445+
426446
def test_numpy_compat(self):
427447
# see gh-12811
428448
r = rwindow.Rolling(Series([2, 4, 6]), window=2)

0 commit comments

Comments
 (0)