Skip to content

Commit 71d7435

Browse files
y-pjreback
y-p
authored andcommitted
BUG: rolling_X functions mishandle edges when center = True
1 parent 4a114d2 commit 71d7435

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/stats/moments.py

+29
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,35 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None,
288288
rs = return_hook(result)
289289
if center:
290290
rs = _center_window(rs, window, axis)
291+
# GH2953, fixup edges
292+
if window > 2:
293+
if values.ndim > 1:
294+
# TODO: handle mi vectorized case
295+
pass
296+
else:
297+
# there's an ambiguity on what constitutes
298+
# the "center" when window is even
299+
# we Just close ranks with numpy , see test case
300+
# delta = 1 if window % 2 == 0 else 0
301+
if window % 2 == 0 :
302+
nahead = (window-1)//2 or 1
303+
else:
304+
nahead = (window)//2
305+
306+
# fixup the head
307+
tip = np.append(np.zeros(nahead+1),values[:(2*nahead+1)])
308+
rs[:nahead+1] = calc(tip)[-(nahead+1):][:nahead+1]
309+
310+
# fixup the tail
311+
tip = np.append(values[-(2*nahead+1):],np.zeros(nahead))
312+
rs[-(nahead):] = calc(tip)[-(nahead):]
313+
314+
if minp > 0:
315+
d = minp - nahead-1
316+
if d > 0:
317+
rs[:d] = NaN
318+
rs[-(d):] = NaN
319+
291320
return rs
292321

293322

0 commit comments

Comments
 (0)