|
7 | 7 | from pandas import DataFrame, Index, MultiIndex, Series, isna, notna
|
8 | 8 | import pandas._testing as tm
|
9 | 9 | from pandas.tests.window.common import (
|
10 |
| - Base, |
11 | 10 | moments_consistency_cov_data,
|
12 | 11 | moments_consistency_is_constant,
|
13 | 12 | moments_consistency_mock_mean,
|
|
18 | 17 | )
|
19 | 18 |
|
20 | 19 |
|
21 |
| -class TestExpandingMomentsConsistency(Base): |
22 |
| - def setup_method(self, method): |
23 |
| - self._create_data() |
| 20 | +def _check_expanding( |
| 21 | + func, static_comp, preserve_nan=True, series=None, frame=None, nan_locs=None |
| 22 | +): |
24 | 23 |
|
25 |
| - def test_expanding_corr(self): |
26 |
| - A = self.series.dropna() |
27 |
| - B = (A + randn(len(A)))[:-5] |
| 24 | + series_result = func(series) |
| 25 | + assert isinstance(series_result, Series) |
| 26 | + frame_result = func(frame) |
| 27 | + assert isinstance(frame_result, DataFrame) |
28 | 28 |
|
29 |
| - result = A.expanding().corr(B) |
| 29 | + result = func(series) |
| 30 | + tm.assert_almost_equal(result[10], static_comp(series[:11])) |
30 | 31 |
|
31 |
| - rolling_result = A.rolling(window=len(A), min_periods=1).corr(B) |
| 32 | + if preserve_nan: |
| 33 | + assert result.iloc[nan_locs].isna().all() |
32 | 34 |
|
33 |
| - tm.assert_almost_equal(rolling_result, result) |
34 | 35 |
|
35 |
| - def test_expanding_count(self): |
36 |
| - result = self.series.expanding(min_periods=0).count() |
37 |
| - tm.assert_almost_equal( |
38 |
| - result, self.series.rolling(window=len(self.series), min_periods=0).count() |
39 |
| - ) |
| 36 | +def _check_expanding_has_min_periods(func, static_comp, has_min_periods): |
| 37 | + ser = Series(randn(50)) |
40 | 38 |
|
41 |
| - def test_expanding_quantile(self): |
42 |
| - result = self.series.expanding().quantile(0.5) |
| 39 | + if has_min_periods: |
| 40 | + result = func(ser, min_periods=30) |
| 41 | + assert result[:29].isna().all() |
| 42 | + tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) |
43 | 43 |
|
44 |
| - rolling_result = self.series.rolling( |
45 |
| - window=len(self.series), min_periods=1 |
46 |
| - ).quantile(0.5) |
| 44 | + # min_periods is working correctly |
| 45 | + result = func(ser, min_periods=15) |
| 46 | + assert isna(result.iloc[13]) |
| 47 | + assert notna(result.iloc[14]) |
47 | 48 |
|
48 |
| - tm.assert_almost_equal(result, rolling_result) |
| 49 | + ser2 = Series(randn(20)) |
| 50 | + result = func(ser2, min_periods=5) |
| 51 | + assert isna(result[3]) |
| 52 | + assert notna(result[4]) |
49 | 53 |
|
50 |
| - def test_expanding_cov(self): |
51 |
| - A = self.series |
52 |
| - B = (A + randn(len(A)))[:-5] |
| 54 | + # min_periods=0 |
| 55 | + result0 = func(ser, min_periods=0) |
| 56 | + result1 = func(ser, min_periods=1) |
| 57 | + tm.assert_almost_equal(result0, result1) |
| 58 | + else: |
| 59 | + result = func(ser) |
| 60 | + tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) |
53 | 61 |
|
54 |
| - result = A.expanding().cov(B) |
55 | 62 |
|
56 |
| - rolling_result = A.rolling(window=len(A), min_periods=1).cov(B) |
| 63 | +def test_expanding_corr(series): |
| 64 | + A = series.dropna() |
| 65 | + B = (A + randn(len(A)))[:-5] |
57 | 66 |
|
58 |
| - tm.assert_almost_equal(rolling_result, result) |
| 67 | + result = A.expanding().corr(B) |
59 | 68 |
|
60 |
| - def test_expanding_cov_pairwise(self): |
61 |
| - result = self.frame.expanding().corr() |
| 69 | + rolling_result = A.rolling(window=len(A), min_periods=1).corr(B) |
62 | 70 |
|
63 |
| - rolling_result = self.frame.rolling( |
64 |
| - window=len(self.frame), min_periods=1 |
65 |
| - ).corr() |
| 71 | + tm.assert_almost_equal(rolling_result, result) |
66 | 72 |
|
67 |
| - tm.assert_frame_equal(result, rolling_result) |
68 | 73 |
|
69 |
| - def test_expanding_corr_pairwise(self): |
70 |
| - result = self.frame.expanding().corr() |
| 74 | +def test_expanding_count(series): |
| 75 | + result = series.expanding(min_periods=0).count() |
| 76 | + tm.assert_almost_equal( |
| 77 | + result, series.rolling(window=len(series), min_periods=0).count() |
| 78 | + ) |
71 | 79 |
|
72 |
| - rolling_result = self.frame.rolling( |
73 |
| - window=len(self.frame), min_periods=1 |
74 |
| - ).corr() |
75 |
| - tm.assert_frame_equal(result, rolling_result) |
76 | 80 |
|
77 |
| - @pytest.mark.parametrize("has_min_periods", [True, False]) |
78 |
| - @pytest.mark.parametrize( |
79 |
| - "func,static_comp", |
80 |
| - [("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)], |
81 |
| - ids=["sum", "mean", "max", "min"], |
82 |
| - ) |
83 |
| - def test_expanding_func(self, func, static_comp, has_min_periods): |
84 |
| - def expanding_func(x, min_periods=1, center=False, axis=0): |
85 |
| - exp = x.expanding(min_periods=min_periods, center=center, axis=axis) |
86 |
| - return getattr(exp, func)() |
87 |
| - |
88 |
| - self._check_expanding(expanding_func, static_comp, preserve_nan=False) |
89 |
| - self._check_expanding_has_min_periods( |
90 |
| - expanding_func, static_comp, has_min_periods |
91 |
| - ) |
| 81 | +def test_expanding_quantile(series): |
| 82 | + result = series.expanding().quantile(0.5) |
| 83 | + |
| 84 | + rolling_result = series.rolling(window=len(series), min_periods=1).quantile(0.5) |
| 85 | + |
| 86 | + tm.assert_almost_equal(result, rolling_result) |
| 87 | + |
92 | 88 |
|
93 |
| - @pytest.mark.parametrize("has_min_periods", [True, False]) |
94 |
| - def test_expanding_apply(self, engine_and_raw, has_min_periods): |
| 89 | +def test_expanding_cov(series): |
| 90 | + A = series |
| 91 | + B = (A + randn(len(A)))[:-5] |
95 | 92 |
|
96 |
| - engine, raw = engine_and_raw |
| 93 | + result = A.expanding().cov(B) |
97 | 94 |
|
98 |
| - def expanding_mean(x, min_periods=1): |
| 95 | + rolling_result = A.rolling(window=len(A), min_periods=1).cov(B) |
99 | 96 |
|
100 |
| - exp = x.expanding(min_periods=min_periods) |
101 |
| - result = exp.apply(lambda x: x.mean(), raw=raw, engine=engine) |
102 |
| - return result |
| 97 | + tm.assert_almost_equal(rolling_result, result) |
103 | 98 |
|
104 |
| - # TODO(jreback), needed to add preserve_nan=False |
105 |
| - # here to make this pass |
106 |
| - self._check_expanding(expanding_mean, np.mean, preserve_nan=False) |
107 |
| - self._check_expanding_has_min_periods(expanding_mean, np.mean, has_min_periods) |
108 | 99 |
|
109 |
| - def _check_expanding(self, func, static_comp, preserve_nan=True): |
| 100 | +def test_expanding_cov_pairwise(frame): |
| 101 | + result = frame.expanding().cov() |
110 | 102 |
|
111 |
| - series_result = func(self.series) |
112 |
| - assert isinstance(series_result, Series) |
113 |
| - frame_result = func(self.frame) |
114 |
| - assert isinstance(frame_result, DataFrame) |
| 103 | + rolling_result = frame.rolling(window=len(frame), min_periods=1).cov() |
115 | 104 |
|
116 |
| - result = func(self.series) |
117 |
| - tm.assert_almost_equal(result[10], static_comp(self.series[:11])) |
| 105 | + tm.assert_frame_equal(result, rolling_result) |
118 | 106 |
|
119 |
| - if preserve_nan: |
120 |
| - assert result.iloc[self._nan_locs].isna().all() |
121 | 107 |
|
122 |
| - def _check_expanding_has_min_periods(self, func, static_comp, has_min_periods): |
123 |
| - ser = Series(randn(50)) |
| 108 | +def test_expanding_corr_pairwise(frame): |
| 109 | + result = frame.expanding().corr() |
124 | 110 |
|
125 |
| - if has_min_periods: |
126 |
| - result = func(ser, min_periods=30) |
127 |
| - assert result[:29].isna().all() |
128 |
| - tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) |
| 111 | + rolling_result = frame.rolling(window=len(frame), min_periods=1).corr() |
| 112 | + tm.assert_frame_equal(result, rolling_result) |
129 | 113 |
|
130 |
| - # min_periods is working correctly |
131 |
| - result = func(ser, min_periods=15) |
132 |
| - assert isna(result.iloc[13]) |
133 |
| - assert notna(result.iloc[14]) |
134 | 114 |
|
135 |
| - ser2 = Series(randn(20)) |
136 |
| - result = func(ser2, min_periods=5) |
137 |
| - assert isna(result[3]) |
138 |
| - assert notna(result[4]) |
| 115 | +@pytest.mark.parametrize("has_min_periods", [True, False]) |
| 116 | +@pytest.mark.parametrize( |
| 117 | + "func,static_comp", |
| 118 | + [("sum", np.sum), ("mean", np.mean), ("max", np.max), ("min", np.min)], |
| 119 | + ids=["sum", "mean", "max", "min"], |
| 120 | +) |
| 121 | +def test_expanding_func(func, static_comp, has_min_periods, series, frame, nan_locs): |
| 122 | + def expanding_func(x, min_periods=1, center=False, axis=0): |
| 123 | + exp = x.expanding(min_periods=min_periods, center=center, axis=axis) |
| 124 | + return getattr(exp, func)() |
| 125 | + |
| 126 | + _check_expanding( |
| 127 | + expanding_func, |
| 128 | + static_comp, |
| 129 | + preserve_nan=False, |
| 130 | + series=series, |
| 131 | + frame=frame, |
| 132 | + nan_locs=nan_locs, |
| 133 | + ) |
| 134 | + _check_expanding_has_min_periods(expanding_func, static_comp, has_min_periods) |
| 135 | + |
139 | 136 |
|
140 |
| - # min_periods=0 |
141 |
| - result0 = func(ser, min_periods=0) |
142 |
| - result1 = func(ser, min_periods=1) |
143 |
| - tm.assert_almost_equal(result0, result1) |
144 |
| - else: |
145 |
| - result = func(ser) |
146 |
| - tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) |
| 137 | +@pytest.mark.parametrize("has_min_periods", [True, False]) |
| 138 | +def test_expanding_apply(engine_and_raw, has_min_periods, series, frame, nan_locs): |
| 139 | + |
| 140 | + engine, raw = engine_and_raw |
| 141 | + |
| 142 | + def expanding_mean(x, min_periods=1): |
| 143 | + |
| 144 | + exp = x.expanding(min_periods=min_periods) |
| 145 | + result = exp.apply(lambda x: x.mean(), raw=raw, engine=engine) |
| 146 | + return result |
| 147 | + |
| 148 | + # TODO(jreback), needed to add preserve_nan=False |
| 149 | + # here to make this pass |
| 150 | + _check_expanding( |
| 151 | + expanding_mean, |
| 152 | + np.mean, |
| 153 | + preserve_nan=False, |
| 154 | + series=series, |
| 155 | + frame=frame, |
| 156 | + nan_locs=nan_locs, |
| 157 | + ) |
| 158 | + _check_expanding_has_min_periods(expanding_mean, np.mean, has_min_periods) |
147 | 159 |
|
148 | 160 |
|
149 | 161 | @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4])
|
|
0 commit comments