|
7 | 7 | from pandas.core.groupby.groupby import get_groupby
|
8 | 8 |
|
9 | 9 |
|
10 |
| -class TestGrouperGrouping: |
| 10 | +class TestRolling: |
11 | 11 | def setup_method(self):
|
12 |
| - self.series = Series(np.arange(10)) |
13 | 12 | self.frame = DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})
|
14 | 13 |
|
15 | 14 | def test_mutated(self):
|
@@ -152,68 +151,6 @@ def test_rolling_apply_mutability(self):
|
152 | 151 | result = g.rolling(window=2).sum()
|
153 | 152 | tm.assert_frame_equal(result, expected)
|
154 | 153 |
|
155 |
| - @pytest.mark.parametrize( |
156 |
| - "f", ["sum", "mean", "min", "max", "count", "kurt", "skew"] |
157 |
| - ) |
158 |
| - def test_expanding(self, f): |
159 |
| - g = self.frame.groupby("A") |
160 |
| - r = g.expanding() |
161 |
| - |
162 |
| - result = getattr(r, f)() |
163 |
| - expected = g.apply(lambda x: getattr(x.expanding(), f)()) |
164 |
| - tm.assert_frame_equal(result, expected) |
165 |
| - |
166 |
| - @pytest.mark.parametrize("f", ["std", "var"]) |
167 |
| - def test_expanding_ddof(self, f): |
168 |
| - g = self.frame.groupby("A") |
169 |
| - r = g.expanding() |
170 |
| - |
171 |
| - result = getattr(r, f)(ddof=0) |
172 |
| - expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0)) |
173 |
| - tm.assert_frame_equal(result, expected) |
174 |
| - |
175 |
| - @pytest.mark.parametrize( |
176 |
| - "interpolation", ["linear", "lower", "higher", "midpoint", "nearest"] |
177 |
| - ) |
178 |
| - def test_expanding_quantile(self, interpolation): |
179 |
| - g = self.frame.groupby("A") |
180 |
| - r = g.expanding() |
181 |
| - result = r.quantile(0.4, interpolation=interpolation) |
182 |
| - expected = g.apply( |
183 |
| - lambda x: x.expanding().quantile(0.4, interpolation=interpolation) |
184 |
| - ) |
185 |
| - tm.assert_frame_equal(result, expected) |
186 |
| - |
187 |
| - @pytest.mark.parametrize("f", ["corr", "cov"]) |
188 |
| - def test_expanding_corr_cov(self, f): |
189 |
| - g = self.frame.groupby("A") |
190 |
| - r = g.expanding() |
191 |
| - |
192 |
| - result = getattr(r, f)(self.frame) |
193 |
| - |
194 |
| - def func(x): |
195 |
| - return getattr(x.expanding(), f)(self.frame) |
196 |
| - |
197 |
| - expected = g.apply(func) |
198 |
| - tm.assert_frame_equal(result, expected) |
199 |
| - |
200 |
| - result = getattr(r.B, f)(pairwise=True) |
201 |
| - |
202 |
| - def func(x): |
203 |
| - return getattr(x.B.expanding(), f)(pairwise=True) |
204 |
| - |
205 |
| - expected = g.apply(func) |
206 |
| - tm.assert_series_equal(result, expected) |
207 |
| - |
208 |
| - def test_expanding_apply(self, raw): |
209 |
| - g = self.frame.groupby("A") |
210 |
| - r = g.expanding() |
211 |
| - |
212 |
| - # reduction |
213 |
| - result = r.apply(lambda x: x.sum(), raw=raw) |
214 |
| - expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw)) |
215 |
| - tm.assert_frame_equal(result, expected) |
216 |
| - |
217 | 154 | @pytest.mark.parametrize("expected_value,raw_value", [[1.0, True], [0.0, False]])
|
218 | 155 | def test_groupby_rolling(self, expected_value, raw_value):
|
219 | 156 | # GH 31754
|
@@ -633,6 +570,73 @@ def test_groupby_rolling_index_level_and_column_label(self):
|
633 | 570 | tm.assert_frame_equal(result, expected)
|
634 | 571 |
|
635 | 572 |
|
| 573 | +class TestExpanding: |
| 574 | + def setup_method(self): |
| 575 | + self.frame = DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)}) |
| 576 | + |
| 577 | + @pytest.mark.parametrize( |
| 578 | + "f", ["sum", "mean", "min", "max", "count", "kurt", "skew"] |
| 579 | + ) |
| 580 | + def test_expanding(self, f): |
| 581 | + g = self.frame.groupby("A") |
| 582 | + r = g.expanding() |
| 583 | + |
| 584 | + result = getattr(r, f)() |
| 585 | + expected = g.apply(lambda x: getattr(x.expanding(), f)()) |
| 586 | + tm.assert_frame_equal(result, expected) |
| 587 | + |
| 588 | + @pytest.mark.parametrize("f", ["std", "var"]) |
| 589 | + def test_expanding_ddof(self, f): |
| 590 | + g = self.frame.groupby("A") |
| 591 | + r = g.expanding() |
| 592 | + |
| 593 | + result = getattr(r, f)(ddof=0) |
| 594 | + expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0)) |
| 595 | + tm.assert_frame_equal(result, expected) |
| 596 | + |
| 597 | + @pytest.mark.parametrize( |
| 598 | + "interpolation", ["linear", "lower", "higher", "midpoint", "nearest"] |
| 599 | + ) |
| 600 | + def test_expanding_quantile(self, interpolation): |
| 601 | + g = self.frame.groupby("A") |
| 602 | + r = g.expanding() |
| 603 | + result = r.quantile(0.4, interpolation=interpolation) |
| 604 | + expected = g.apply( |
| 605 | + lambda x: x.expanding().quantile(0.4, interpolation=interpolation) |
| 606 | + ) |
| 607 | + tm.assert_frame_equal(result, expected) |
| 608 | + |
| 609 | + @pytest.mark.parametrize("f", ["corr", "cov"]) |
| 610 | + def test_expanding_corr_cov(self, f): |
| 611 | + g = self.frame.groupby("A") |
| 612 | + r = g.expanding() |
| 613 | + |
| 614 | + result = getattr(r, f)(self.frame) |
| 615 | + |
| 616 | + def func(x): |
| 617 | + return getattr(x.expanding(), f)(self.frame) |
| 618 | + |
| 619 | + expected = g.apply(func) |
| 620 | + tm.assert_frame_equal(result, expected) |
| 621 | + |
| 622 | + result = getattr(r.B, f)(pairwise=True) |
| 623 | + |
| 624 | + def func(x): |
| 625 | + return getattr(x.B.expanding(), f)(pairwise=True) |
| 626 | + |
| 627 | + expected = g.apply(func) |
| 628 | + tm.assert_series_equal(result, expected) |
| 629 | + |
| 630 | + def test_expanding_apply(self, raw): |
| 631 | + g = self.frame.groupby("A") |
| 632 | + r = g.expanding() |
| 633 | + |
| 634 | + # reduction |
| 635 | + result = r.apply(lambda x: x.sum(), raw=raw) |
| 636 | + expected = g.apply(lambda x: x.expanding().apply(lambda y: y.sum(), raw=raw)) |
| 637 | + tm.assert_frame_equal(result, expected) |
| 638 | + |
| 639 | + |
636 | 640 | class TestEWM:
|
637 | 641 | @pytest.mark.parametrize(
|
638 | 642 | "method, expected_data",
|
|
0 commit comments