Skip to content

Commit f165b90

Browse files
FXocenajreback
authored andcommitted
BUG: Rolling apply on DataFrame with Datetime index returns NaN (#17156)
1 parent dbffba8 commit f165b90

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Diff for: doc/source/whatsnew/v0.21.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ Groupby/Resample/Rolling
350350
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
351351
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
352352
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)
353+
- Bug in ``.rolling(...).apply(...)`` with a ``DataFrame`` with a ``DatetimeIndex``, a ``window`` of a timedelta-convertible and ``min_periods >= 1` (:issue:`15305`)
354+
353355

354356
Sparse
355357
^^^^^^

Diff for: pandas/_libs/window.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1428,15 +1428,16 @@ 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+
14311435
start, end, N, win, minp, is_variable = get_window_indexer(input, win,
14321436
minp, index,
14331437
closed,
14341438
floor=0)
1435-
output = np.empty(N, dtype=float)
14361439

1437-
counts = roll_sum(np.concatenate([np.isfinite(input).astype(float),
1438-
np.array([0.] * offset)]),
1439-
win, minp, index, closed)[offset:]
1440+
output = np.empty(N, dtype=float)
14401441

14411442
if is_variable:
14421443

Diff for: 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+
@pytest.mark.parametrize(
427+
'window', [timedelta(days=3), pd.Timedelta(days=3), '3D'])
428+
def test_constructor_with_timedelta_window_and_minperiods(self, window):
429+
# GH 15305
430+
n = 10
431+
df = pd.DataFrame({'value': np.arange(n)},
432+
index=pd.date_range('2017-08-08',
433+
periods=n,
434+
freq="D"))
435+
expected = pd.DataFrame({'value': np.append([np.NaN, 1.],
436+
np.arange(3., 27., 3))},
437+
index=pd.date_range('2017-08-08',
438+
periods=n,
439+
freq="D"))
440+
result_roll_sum = df.rolling(window=window, min_periods=2).sum()
441+
result_roll_generic = df.rolling(window=window,
442+
min_periods=2).apply(sum)
443+
tm.assert_frame_equal(result_roll_sum, expected)
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)