Skip to content

Commit

Permalink
BUG: Rolling apply on DataFrame with Datetime index returns NaN (#17156)
Browse files Browse the repository at this point in the history
  • Loading branch information
FXocena authored and jreback committed Aug 10, 2017
1 parent dbffba8 commit f165b90
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ Groupby/Resample/Rolling
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)
- Bug in ``.rolling(...).apply(...)`` with a ``DataFrame`` with a ``DatetimeIndex``, a ``window`` of a timedelta-convertible and ``min_periods >= 1` (:issue:`15305`)


Sparse
^^^^^^
Expand Down
9 changes: 5 additions & 4 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1428,15 +1428,16 @@ def roll_generic(ndarray[float64_t, cast=True] input,
if n == 0:
return input

counts = roll_sum(np.concatenate([np.isfinite(input).astype(float),
np.array([0.] * offset)]),
win, minp, index, closed)[offset:]

start, end, N, win, minp, is_variable = get_window_indexer(input, win,
minp, index,
closed,
floor=0)
output = np.empty(N, dtype=float)

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

if is_variable:

Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,26 @@ def test_constructor_with_timedelta_window(self):
expected = df.rolling('3D').sum()
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
'window', [timedelta(days=3), pd.Timedelta(days=3), '3D'])
def test_constructor_with_timedelta_window_and_minperiods(self, window):
# GH 15305
n = 10
df = pd.DataFrame({'value': np.arange(n)},
index=pd.date_range('2017-08-08',
periods=n,
freq="D"))
expected = pd.DataFrame({'value': np.append([np.NaN, 1.],
np.arange(3., 27., 3))},
index=pd.date_range('2017-08-08',
periods=n,
freq="D"))
result_roll_sum = df.rolling(window=window, min_periods=2).sum()
result_roll_generic = df.rolling(window=window,
min_periods=2).apply(sum)
tm.assert_frame_equal(result_roll_sum, expected)
tm.assert_frame_equal(result_roll_generic, expected)

def test_numpy_compat(self):
# see gh-12811
r = rwindow.Rolling(Series([2, 4, 6]), window=2)
Expand Down

0 comments on commit f165b90

Please sign in to comment.