|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +from numpy.random import randn |
| 6 | + |
| 7 | +import pandas.core.window as rwindow |
| 8 | +import pandas.util.testing as tm |
| 9 | +from pandas import DataFrame, Series, bdate_range |
| 10 | +from pandas.errors import UnsupportedFunctionCall |
| 11 | + |
| 12 | +N, K = 100, 10 |
| 13 | + |
| 14 | + |
| 15 | +def assert_equal(left, right): |
| 16 | + if isinstance(left, Series): |
| 17 | + tm.assert_series_equal(left, right) |
| 18 | + else: |
| 19 | + tm.assert_frame_equal(left, right) |
| 20 | + |
| 21 | + |
| 22 | +class Base(object): |
| 23 | + |
| 24 | + _nan_locs = np.arange(20, 40) |
| 25 | + _inf_locs = np.array([]) |
| 26 | + |
| 27 | + def _create_data(self): |
| 28 | + arr = randn(N) |
| 29 | + arr[self._nan_locs] = np.NaN |
| 30 | + |
| 31 | + self.arr = arr |
| 32 | + self.rng = bdate_range(datetime(2009, 1, 1), periods=N) |
| 33 | + self.series = Series(arr.copy(), index=self.rng) |
| 34 | + self.frame = DataFrame(randn(N, K), index=self.rng, |
| 35 | + columns=np.arange(K)) |
| 36 | + |
| 37 | + |
| 38 | +class TestEWM(Base): |
| 39 | + |
| 40 | + def setup_method(self, method): |
| 41 | + self._create_data() |
| 42 | + |
| 43 | + def test_doc_string(self): |
| 44 | + |
| 45 | + df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) |
| 46 | + df |
| 47 | + df.ewm(com=0.5).mean() |
| 48 | + |
| 49 | + def test_constructor(self): |
| 50 | + for o in [self.series, self.frame]: |
| 51 | + c = o.ewm |
| 52 | + |
| 53 | + # valid |
| 54 | + c(com=0.5) |
| 55 | + c(span=1.5) |
| 56 | + c(alpha=0.5) |
| 57 | + c(halflife=0.75) |
| 58 | + c(com=0.5, span=None) |
| 59 | + c(alpha=0.5, com=None) |
| 60 | + c(halflife=0.75, alpha=None) |
| 61 | + |
| 62 | + # not valid: mutually exclusive |
| 63 | + with pytest.raises(ValueError): |
| 64 | + c(com=0.5, alpha=0.5) |
| 65 | + with pytest.raises(ValueError): |
| 66 | + c(span=1.5, halflife=0.75) |
| 67 | + with pytest.raises(ValueError): |
| 68 | + c(alpha=0.5, span=1.5) |
| 69 | + |
| 70 | + # not valid: com < 0 |
| 71 | + with pytest.raises(ValueError): |
| 72 | + c(com=-0.5) |
| 73 | + |
| 74 | + # not valid: span < 1 |
| 75 | + with pytest.raises(ValueError): |
| 76 | + c(span=0.5) |
| 77 | + |
| 78 | + # not valid: halflife <= 0 |
| 79 | + with pytest.raises(ValueError): |
| 80 | + c(halflife=0) |
| 81 | + |
| 82 | + # not valid: alpha <= 0 or alpha > 1 |
| 83 | + for alpha in (-0.5, 1.5): |
| 84 | + with pytest.raises(ValueError): |
| 85 | + c(alpha=alpha) |
| 86 | + |
| 87 | + def test_numpy_compat(self): |
| 88 | + # see gh-12811 |
| 89 | + e = rwindow.EWM(Series([2, 4, 6]), alpha=0.5) |
| 90 | + |
| 91 | + msg = "numpy operations are not valid with window objects" |
| 92 | + |
| 93 | + for func in ('std', 'mean', 'var'): |
| 94 | + tm.assert_raises_regex(UnsupportedFunctionCall, msg, |
| 95 | + getattr(e, func), 1, 2, 3) |
| 96 | + tm.assert_raises_regex(UnsupportedFunctionCall, msg, |
| 97 | + getattr(e, func), dtype=np.float64) |
0 commit comments