Skip to content

Commit 18788f4

Browse files
committed
BUG: fix NA handling in ewma adjustment. close #2128
1 parent 86ab2e9 commit 18788f4

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pandas 0.9.1
7878
- Raise Exception when xs view not possible of MultiIndex'd DataFrame (#2117)
7979
- Fix groupby(...).first() issue with datetime64 (#2133)
8080
- Better floating point error robustness in some rolling_* functions (#2114)
81+
- Fix ewma NA handling in the middle of Series (#2128)
8182
8283
pandas 0.9.0
8384
============

pandas/src/moments.pyx

+8-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ def ewma(ndarray[double_t] input, double_t com, int adjust):
265265
oldw = 1. - neww
266266
adj = oldw
267267

268-
output[0] = neww * input[0]
268+
if adjust:
269+
output[0] = neww * input[0]
270+
else:
271+
output[0] = input[0]
269272

270273
for i from 1 <= i < N:
271274
cur = input[i]
@@ -282,10 +285,13 @@ def ewma(ndarray[double_t] input, double_t com, int adjust):
282285
if adjust:
283286
for i from 0 <= i < N:
284287
cur = input[i]
285-
output[i] = output[i] / (1. - adj)
286288

287289
if cur == cur:
290+
output[i] = output[i] / (1. - adj)
288291
adj *= oldw
292+
else:
293+
if i >= 1:
294+
output[i] = output[i - 1]
289295

290296
return output
291297

pandas/stats/tests/test_moments.py

+6
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ def test_ewma(self):
299299
result = mom.ewma(arr, span=100, adjust=False).sum()
300300
self.assert_(np.abs(result - 1) < 1e-2)
301301

302+
def test_ewma_nan_handling(self):
303+
s = Series([1.] + [np.nan] * 5 + [1.])
304+
305+
result = mom.ewma(s, com=5)
306+
assert_almost_equal(result, [1] * len(s))
307+
302308
def test_ewmvar(self):
303309
self._check_ew(mom.ewmvar)
304310

0 commit comments

Comments
 (0)