|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +from numpy.random import randn |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +import pandas.core.window as rwindow |
| 9 | +import pandas.util.testing as tm |
| 10 | +from pandas import DataFrame, Series, bdate_range |
| 11 | +from pandas.errors import UnsupportedFunctionCall |
| 12 | + |
| 13 | +N, K = 100, 10 |
| 14 | + |
| 15 | + |
| 16 | +def assert_equal(left, right): |
| 17 | + if isinstance(left, Series): |
| 18 | + tm.assert_series_equal(left, right) |
| 19 | + else: |
| 20 | + tm.assert_frame_equal(left, right) |
| 21 | + |
| 22 | + |
| 23 | +class Base(object): |
| 24 | + |
| 25 | + _nan_locs = np.arange(20, 40) |
| 26 | + _inf_locs = np.array([]) |
| 27 | + |
| 28 | + def _create_data(self): |
| 29 | + arr = randn(N) |
| 30 | + arr[self._nan_locs] = np.NaN |
| 31 | + |
| 32 | + self.arr = arr |
| 33 | + self.rng = bdate_range(datetime(2009, 1, 1), periods=N) |
| 34 | + self.series = Series(arr.copy(), index=self.rng) |
| 35 | + self.frame = DataFrame(randn(N, K), index=self.rng, |
| 36 | + columns=np.arange(K)) |
| 37 | + |
| 38 | + |
| 39 | +class TestExpanding(Base): |
| 40 | + |
| 41 | + def setup_method(self, method): |
| 42 | + self._create_data() |
| 43 | + |
| 44 | + def test_doc_string(self): |
| 45 | + |
| 46 | + df = DataFrame({'B': [0, 1, 2, np.nan, 4]}) |
| 47 | + df |
| 48 | + df.expanding(2).sum() |
| 49 | + |
| 50 | + def test_constructor(self): |
| 51 | + # GH 12669 |
| 52 | + |
| 53 | + for o in [self.series, self.frame]: |
| 54 | + c = o.expanding |
| 55 | + |
| 56 | + # valid |
| 57 | + c(min_periods=1) |
| 58 | + c(min_periods=1, center=True) |
| 59 | + c(min_periods=1, center=False) |
| 60 | + |
| 61 | + # not valid |
| 62 | + for w in [2., 'foo', np.array([2])]: |
| 63 | + with pytest.raises(ValueError): |
| 64 | + c(min_periods=w) |
| 65 | + with pytest.raises(ValueError): |
| 66 | + c(min_periods=1, center=w) |
| 67 | + |
| 68 | + def test_numpy_compat(self): |
| 69 | + # see gh-12811 |
| 70 | + e = rwindow.Expanding(Series([2, 4, 6]), window=2) |
| 71 | + |
| 72 | + msg = "numpy operations are not valid with window objects" |
| 73 | + |
| 74 | + for func in ('std', 'mean', 'sum', 'max', 'min', 'var'): |
| 75 | + tm.assert_raises_regex(UnsupportedFunctionCall, msg, |
| 76 | + getattr(e, func), 1, 2, 3) |
| 77 | + tm.assert_raises_regex(UnsupportedFunctionCall, msg, |
| 78 | + getattr(e, func), dtype=np.float64) |
| 79 | + |
| 80 | + @pytest.mark.parametrize( |
| 81 | + 'expander', |
| 82 | + [1, pytest.param('ls', marks=pytest.mark.xfail( |
| 83 | + reason='GH 16425 expanding with ' |
| 84 | + 'offset not supported'))]) |
| 85 | + def test_empty_df_expanding(self, expander): |
| 86 | + # GH 15819 Verifies that datetime and integer expanding windows can be |
| 87 | + # applied to empty DataFrames |
| 88 | + |
| 89 | + expected = DataFrame() |
| 90 | + result = DataFrame().expanding(expander).sum() |
| 91 | + tm.assert_frame_equal(result, expected) |
| 92 | + |
| 93 | + # Verifies that datetime and integer expanding windows can be applied |
| 94 | + # to empty DataFrames with datetime index |
| 95 | + expected = DataFrame(index=pd.DatetimeIndex([])) |
| 96 | + result = DataFrame( |
| 97 | + index=pd.DatetimeIndex([])).expanding(expander).sum() |
| 98 | + tm.assert_frame_equal(result, expected) |
| 99 | + |
| 100 | + def test_missing_minp_zero(self): |
| 101 | + # https://github.com/pandas-dev/pandas/pull/18921 |
| 102 | + # minp=0 |
| 103 | + x = pd.Series([np.nan]) |
| 104 | + result = x.expanding(min_periods=0).sum() |
| 105 | + expected = pd.Series([0.0]) |
| 106 | + tm.assert_series_equal(result, expected) |
| 107 | + |
| 108 | + # minp=1 |
| 109 | + result = x.expanding(min_periods=1).sum() |
| 110 | + expected = pd.Series([np.nan]) |
| 111 | + tm.assert_series_equal(result, expected) |
0 commit comments