From 8c89d6003c40b25a13e18072defe414cffbbda94 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Sun, 12 Nov 2023 19:33:42 +0700 Subject: [PATCH 1/2] add test --- pandas/tests/groupby/test_grouping.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 8c2b95ba631ee..4f43261d58e6c 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -297,6 +297,23 @@ def test_grouper_creation_bug3(self): expected = ser.groupby(level="one").sum() tm.assert_series_equal(result, expected) + @pytest.mark.parametrize('func', [False, True]) + def test_grouper_returning_tuples(self, func): + # GH 22257 , both with dict and with callable + df = pd.DataFrame({'X': ['A', 'B', 'A', 'B'], 'Y': [1, 4, 3, 2]}) + mapping = dict(zip(range(4), [('C', 5), ('D', 6)] * 2)) + + if func: + gb = df.groupby(by=lambda idx: mapping[idx], sort=False) + else: + gb = df.groupby(by=mapping, sort=False) + + name, expected = list(gb)[0] + assert name == ('C', 5) + result = gb.get_group(name) + + tm.assert_frame_equal(result, expected) + def test_grouper_column_and_index(self): # GH 14327 From 2b3c2814a0ab4d373da28dfb294fd1bad2ed9a38 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Sun, 12 Nov 2023 19:52:59 +0700 Subject: [PATCH 2/2] lint --- pandas/tests/groupby/test_grouping.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 4f43261d58e6c..01768582299eb 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -297,19 +297,19 @@ def test_grouper_creation_bug3(self): expected = ser.groupby(level="one").sum() tm.assert_series_equal(result, expected) - @pytest.mark.parametrize('func', [False, True]) + @pytest.mark.parametrize("func", [False, True]) def test_grouper_returning_tuples(self, func): # GH 22257 , both with dict and with callable - df = pd.DataFrame({'X': ['A', 'B', 'A', 'B'], 'Y': [1, 4, 3, 2]}) - mapping = dict(zip(range(4), [('C', 5), ('D', 6)] * 2)) + df = DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]}) + mapping = dict(zip(range(4), [("C", 5), ("D", 6)] * 2)) if func: gb = df.groupby(by=lambda idx: mapping[idx], sort=False) else: gb = df.groupby(by=mapping, sort=False) - name, expected = list(gb)[0] - assert name == ('C', 5) + name, expected = next(iter(gb)) + assert name == ("C", 5) result = gb.get_group(name) tm.assert_frame_equal(result, expected)