From 835233180aff90d8ee4aa65a95f6daded84a6cfd Mon Sep 17 00:00:00 2001 From: Abdullah Ihsan Secer Date: Tue, 19 Sep 2023 11:40:15 +0100 Subject: [PATCH] TST: Test GroupBy.__getitem__ with a column from grouper (#55193) --- pandas/tests/groupby/test_grouping.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index d05b60fd56b5f..9e899bf453548 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -131,6 +131,20 @@ def test_getitem_single_column(self): tm.assert_series_equal(result, expected) + @pytest.mark.parametrize( + "func", [lambda x: x.sum(), lambda x: x.agg(lambda y: y.sum())] + ) + def test_getitem_from_grouper(self, func): + # GH 50383 + df = DataFrame({"a": [1, 1, 2], "b": 3, "c": 4, "d": 5}) + gb = df.groupby(["a", "b"])[["a", "c"]] + + idx = MultiIndex.from_tuples([(1, 3), (2, 3)], names=["a", "b"]) + expected = DataFrame({"a": [2, 2], "c": [8, 4]}, index=idx) + result = func(gb) + + tm.assert_frame_equal(result, expected) + def test_indices_grouped_by_tuple_with_lambda(self): # GH 36158 df = DataFrame(