diff --git a/RELEASE.rst b/RELEASE.rst index fcd8bf1546a47..1446ea560f379 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -78,6 +78,7 @@ pandas 0.9.1 - Raise Exception when xs view not possible of MultiIndex'd DataFrame (#2117) - Fix groupby(...).first() issue with datetime64 (#2133) - Better floating point error robustness in some rolling_* functions (#2114) + - Fix ewma NA handling in the middle of Series (#2128) pandas 0.9.0 ============ diff --git a/pandas/src/moments.pyx b/pandas/src/moments.pyx index a97e3cd9c262a..0375446834f2b 100644 --- a/pandas/src/moments.pyx +++ b/pandas/src/moments.pyx @@ -265,7 +265,10 @@ def ewma(ndarray[double_t] input, double_t com, int adjust): oldw = 1. - neww adj = oldw - output[0] = neww * input[0] + if adjust: + output[0] = neww * input[0] + else: + output[0] = input[0] for i from 1 <= i < N: cur = input[i] @@ -282,10 +285,13 @@ def ewma(ndarray[double_t] input, double_t com, int adjust): if adjust: for i from 0 <= i < N: cur = input[i] - output[i] = output[i] / (1. - adj) if cur == cur: + output[i] = output[i] / (1. - adj) adj *= oldw + else: + if i >= 1: + output[i] = output[i - 1] return output diff --git a/pandas/stats/tests/test_moments.py b/pandas/stats/tests/test_moments.py index 59fb5a23b2a4c..abb160fc0d00b 100644 --- a/pandas/stats/tests/test_moments.py +++ b/pandas/stats/tests/test_moments.py @@ -299,6 +299,12 @@ def test_ewma(self): result = mom.ewma(arr, span=100, adjust=False).sum() self.assert_(np.abs(result - 1) < 1e-2) + def test_ewma_nan_handling(self): + s = Series([1.] + [np.nan] * 5 + [1.]) + + result = mom.ewma(s, com=5) + assert_almost_equal(result, [1] * len(s)) + def test_ewmvar(self): self._check_ew(mom.ewmvar)