Skip to content

Commit

Permalink
BUG: fix NA handling in ewma adjustment. close pandas-dev#2128
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm authored and Joao Natali committed Nov 10, 2012
1 parent 4fa7906 commit 7199bc8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand Down
10 changes: 8 additions & 2 deletions pandas/src/moments.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions pandas/stats/tests/test_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 7199bc8

Please sign in to comment.