Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Rolling apply on DataFrame with Datetime index returns NaN #17156

Merged
merged 11 commits into from
Aug 10, 2017
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 @@ -349,6 +349,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