Skip to content

Commit 68251c2

Browse files
committed
BUG: handling of 0-length arrays in ewm* functions close #1900
1 parent 24e8fc0 commit 68251c2

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pandas 0.9.0
168168
- Support datetime.date again in DateOffset.rollback/rollforward
169169
- Raise Exception if set passed to Series constructor (#1913)
170170
- Add TypeError when appending HDFStore table w/ wrong index type (#1881)
171+
- Don't raise exception on empty inputs in EW functions (e.g. ewma) (#1900)
171172

172173
pandas 0.8.1
173174
============

pandas/src/moments.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ def ewma(ndarray[double_t] input, double_t com, int adjust):
258258

259259
cdef ndarray[double_t] output = np.empty(N, dtype=float)
260260

261+
if N == 0:
262+
return output
261263

262264
neww = 1. / (1. + com)
263265
oldw = 1. - neww

pandas/stats/moments.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def _ewma(v):
311311

312312
def _first_valid_index(arr):
313313
# argmax scans from left
314-
return notnull(arr).argmax()
314+
return notnull(arr).argmax() if len(arr) else 0
315315

316316
@Substitution("Exponentially-weighted moving variance", _unary_arg, _bias_doc)
317317
@Appender(_ewm_doc)

pandas/stats/tests/test_moments.py

+8
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ def test_ewma_span_com_args(self):
293293
self.assertRaises(Exception, mom.ewma, self.arr, com=9.5, span=20)
294294
self.assertRaises(Exception, mom.ewma, self.arr)
295295

296+
def test_ew_empty_arrays(self):
297+
arr = np.array([], dtype=np.float64)
298+
299+
funcs = [mom.ewma, mom.ewmvol, mom.ewmvar]
300+
for f in funcs:
301+
result = f(arr, 3)
302+
assert_almost_equal(result, arr)
303+
296304
def _check_ew(self, func):
297305
self._check_ew_ndarray(func)
298306
self._check_ew_structures(func)

0 commit comments

Comments
 (0)