|
6 | 6 | DatetimeIndex,
|
7 | 7 | Index,
|
8 | 8 | MultiIndex,
|
| 9 | + NamedAgg, |
9 | 10 | Series,
|
10 | 11 | Timestamp,
|
11 | 12 | date_range,
|
@@ -489,6 +490,36 @@ def test_groupby_rolling_subset_with_closed(self):
|
489 | 490 | )
|
490 | 491 | tm.assert_series_equal(result, expected)
|
491 | 492 |
|
| 493 | + def test_groupby_rolling_agg_namedagg(self): |
| 494 | + # GH#28333 |
| 495 | + df = DataFrame( |
| 496 | + { |
| 497 | + "kind": ["cat", "dog", "cat", "dog", "cat", "dog"], |
| 498 | + "height": [9.1, 6.0, 9.5, 34.0, 12.0, 8.0], |
| 499 | + "weight": [7.9, 7.5, 9.9, 198.0, 10.0, 42.0], |
| 500 | + } |
| 501 | + ) |
| 502 | + result = ( |
| 503 | + df.groupby("kind") |
| 504 | + .rolling(2) |
| 505 | + .agg( |
| 506 | + total_weight=NamedAgg(column="weight", aggfunc=sum), |
| 507 | + min_height=NamedAgg(column="height", aggfunc=min), |
| 508 | + ) |
| 509 | + ) |
| 510 | + expected = DataFrame( |
| 511 | + { |
| 512 | + "total_weight": [np.nan, 17.8, 19.9, np.nan, 205.5, 240.0], |
| 513 | + "min_height": [np.nan, 9.1, 9.5, np.nan, 6.0, 8.0], |
| 514 | + }, |
| 515 | + index=MultiIndex( |
| 516 | + [["cat", "dog"], [0, 1, 2, 3, 4, 5]], |
| 517 | + [[0, 0, 0, 1, 1, 1], [0, 2, 4, 1, 3, 5]], |
| 518 | + names=["kind", None], |
| 519 | + ), |
| 520 | + ) |
| 521 | + tm.assert_frame_equal(result, expected) |
| 522 | + |
492 | 523 | def test_groupby_subset_rolling_subset_with_closed(self):
|
493 | 524 | # GH 35549
|
494 | 525 | df = DataFrame(
|
@@ -1134,6 +1165,36 @@ def test_expanding_apply(self, raw, frame):
|
1134 | 1165 | expected.index = expected_index
|
1135 | 1166 | tm.assert_frame_equal(result, expected)
|
1136 | 1167 |
|
| 1168 | + def test_groupby_expanding_agg_namedagg(self): |
| 1169 | + # GH#28333 |
| 1170 | + df = DataFrame( |
| 1171 | + { |
| 1172 | + "kind": ["cat", "dog", "cat", "dog", "cat", "dog"], |
| 1173 | + "height": [9.1, 6.0, 9.5, 34.0, 12.0, 8.0], |
| 1174 | + "weight": [7.9, 7.5, 9.9, 198.0, 10.0, 42.0], |
| 1175 | + } |
| 1176 | + ) |
| 1177 | + result = ( |
| 1178 | + df.groupby("kind") |
| 1179 | + .expanding(1) |
| 1180 | + .agg( |
| 1181 | + total_weight=NamedAgg(column="weight", aggfunc=sum), |
| 1182 | + min_height=NamedAgg(column="height", aggfunc=min), |
| 1183 | + ) |
| 1184 | + ) |
| 1185 | + expected = DataFrame( |
| 1186 | + { |
| 1187 | + "total_weight": [7.9, 17.8, 27.8, 7.5, 205.5, 247.5], |
| 1188 | + "min_height": [9.1, 9.1, 9.1, 6.0, 6.0, 6.0], |
| 1189 | + }, |
| 1190 | + index=MultiIndex( |
| 1191 | + [["cat", "dog"], [0, 1, 2, 3, 4, 5]], |
| 1192 | + [[0, 0, 0, 1, 1, 1], [0, 2, 4, 1, 3, 5]], |
| 1193 | + names=["kind", None], |
| 1194 | + ), |
| 1195 | + ) |
| 1196 | + tm.assert_frame_equal(result, expected) |
| 1197 | + |
1137 | 1198 |
|
1138 | 1199 | class TestEWM:
|
1139 | 1200 | @pytest.mark.parametrize(
|
@@ -1162,6 +1223,41 @@ def test_methods(self, method, expected_data):
|
1162 | 1223 | )
|
1163 | 1224 | tm.assert_frame_equal(result, expected)
|
1164 | 1225 |
|
| 1226 | + def test_groupby_ewm_agg_namedagg(self): |
| 1227 | + # GH#28333 |
| 1228 | + df = DataFrame({"A": ["a"] * 4, "B": range(4)}) |
| 1229 | + result = ( |
| 1230 | + df.groupby("A") |
| 1231 | + .ewm(com=1.0) |
| 1232 | + .agg( |
| 1233 | + B_mean=NamedAgg(column="B", aggfunc="mean"), |
| 1234 | + B_std=NamedAgg(column="B", aggfunc="std"), |
| 1235 | + B_var=NamedAgg(column="B", aggfunc="var"), |
| 1236 | + ) |
| 1237 | + ) |
| 1238 | + expected = DataFrame( |
| 1239 | + { |
| 1240 | + "B_mean": [ |
| 1241 | + 0.0, |
| 1242 | + 0.6666666666666666, |
| 1243 | + 1.4285714285714286, |
| 1244 | + 2.2666666666666666, |
| 1245 | + ], |
| 1246 | + "B_std": [np.nan, 0.707107, 0.963624, 1.177164], |
| 1247 | + "B_var": [np.nan, 0.5, 0.9285714285714286, 1.3857142857142857], |
| 1248 | + }, |
| 1249 | + index=MultiIndex.from_tuples( |
| 1250 | + [ |
| 1251 | + ("a", 0), |
| 1252 | + ("a", 1), |
| 1253 | + ("a", 2), |
| 1254 | + ("a", 3), |
| 1255 | + ], |
| 1256 | + names=["A", None], |
| 1257 | + ), |
| 1258 | + ) |
| 1259 | + tm.assert_frame_equal(result, expected) |
| 1260 | + |
1165 | 1261 | @pytest.mark.parametrize(
|
1166 | 1262 | "method, expected_data",
|
1167 | 1263 | [["corr", [np.nan, 1.0, 1.0, 1]], ["cov", [np.nan, 0.5, 0.928571, 1.385714]]],
|
|
0 commit comments