From b3dc51559bfe40ae116dc6d2f987621172e8b11d Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Thu, 30 Jun 2022 18:18:38 -0700 Subject: [PATCH 1/5] TST: Test aggregate with list values #25581 --- pandas/tests/groupby/aggregate/test_aggregate.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index d52b6ceaf8990..8bf0ff584fe6c 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1413,3 +1413,13 @@ def test_multi_axis_1_raises(func): gb = df.groupby("a", axis=1) with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): gb.agg(func) + +def test_agg_of_list(): + # GH#25581 + df1 = pd.DataFrame([[20, 'A'], [20, 'B'], [30, 'C']]) + result = df1.groupby(0).agg(pd.Series.mode) + + expected = pd.DataFrame({0: [20, 30], 1: [['A', 'B'], 'C']}) + expected = expected.set_index(0) + + tm.assert_frame_equal(result, expected) From a0f1dcba09efc0135ab7890373384189f2adda3d Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Fri, 1 Jul 2022 16:54:44 -0700 Subject: [PATCH 2/5] TST: Fixed PEP 8 warnings #25581 --- pandas/tests/groupby/aggregate/test_aggregate.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 8bf0ff584fe6c..00ebe6f9e0194 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1414,12 +1414,13 @@ def test_multi_axis_1_raises(func): with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): gb.agg(func) + def test_agg_of_list(): # GH#25581 - df1 = pd.DataFrame([[20, 'A'], [20, 'B'], [30, 'C']]) + df1 = pd.DataFrame([[20, "A"], [20, "B"], [30, "C"]]) result = df1.groupby(0).agg(pd.Series.mode) - expected = pd.DataFrame({0: [20, 30], 1: [['A', 'B'], 'C']}) + expected = pd.DataFrame({0: [20, 30], 1: [["A", "B"], "C"]}) expected = expected.set_index(0) tm.assert_frame_equal(result, expected) From 1fd0a9393ddc7b0c2afabfa4dc56e6c50c254917 Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Fri, 1 Jul 2022 17:20:58 -0700 Subject: [PATCH 3/5] TST: Fixed Flake8 Error #25581 --- pandas/tests/groupby/aggregate/test_aggregate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 00ebe6f9e0194..8dc1b33ddd4c9 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1417,10 +1417,10 @@ def test_multi_axis_1_raises(func): def test_agg_of_list(): # GH#25581 - df1 = pd.DataFrame([[20, "A"], [20, "B"], [30, "C"]]) - result = df1.groupby(0).agg(pd.Series.mode) + df1 = DataFrame([[20, "A"], [20, "B"], [30, "C"]]) + result = df1.groupby(0).agg(Series.mode) - expected = pd.DataFrame({0: [20, 30], 1: [["A", "B"], "C"]}) + expected = DataFrame({0: [20, 30], 1: [["A", "B"], "C"]}) expected = expected.set_index(0) tm.assert_frame_equal(result, expected) From 0e208c8215acd4bfacb53cfe1b43954b441659fd Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Mon, 4 Jul 2022 15:36:54 -0700 Subject: [PATCH 4/5] TST: Added more test cases #25581 --- .../tests/groupby/aggregate/test_aggregate.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 8dc1b33ddd4c9..156d16512d3d2 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1415,12 +1415,23 @@ def test_multi_axis_1_raises(func): gb.agg(func) -def test_agg_of_list(): +@pytest.mark.parametrize( + "test, constant", + [ + ([[20, "A"], [20, "B"], [10, "C"]], {0: [10, 20], 1: ["C", ["A", "B"]]}), + ([[20, "A"], [20, "B"], [30, "C"]], {0: [20, 30], 1: [["A", "B"], "C"]}), + ([["a", 1], ["a", 1], ["b", 2], ["b", 3]], {0: ["a", "b"], 1: [1, [2, 3]]}) + # This test case still fails + # ([["a", 1], ["a", 2], ["b", 3], ["b", 3]], {0: ["a", "b"], 1: [[1, 2], 3]}) + ], +) +def test_agg_of_mode_list(test, constant): # GH#25581 - df1 = DataFrame([[20, "A"], [20, "B"], [30, "C"]]) + df1 = DataFrame(test) result = df1.groupby(0).agg(Series.mode) + # Mode usually only returns 1 value, but can return a list in the case of a tie. - expected = DataFrame({0: [20, 30], 1: [["A", "B"], "C"]}) + expected = DataFrame(constant) expected = expected.set_index(0) tm.assert_frame_equal(result, expected) From 3d3953dd9ae3af976b4c593672403105097e3a38 Mon Sep 17 00:00:00 2001 From: Steven Rotondo Date: Tue, 5 Jul 2022 13:46:43 -0700 Subject: [PATCH 5/5] TST: Changed broken test to xfail #25581 --- pandas/tests/groupby/aggregate/test_aggregate.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 156d16512d3d2..54ee32502bbc9 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1420,9 +1420,12 @@ def test_multi_axis_1_raises(func): [ ([[20, "A"], [20, "B"], [10, "C"]], {0: [10, 20], 1: ["C", ["A", "B"]]}), ([[20, "A"], [20, "B"], [30, "C"]], {0: [20, 30], 1: [["A", "B"], "C"]}), - ([["a", 1], ["a", 1], ["b", 2], ["b", 3]], {0: ["a", "b"], 1: [1, [2, 3]]}) - # This test case still fails - # ([["a", 1], ["a", 2], ["b", 3], ["b", 3]], {0: ["a", "b"], 1: [[1, 2], 3]}) + ([["a", 1], ["a", 1], ["b", 2], ["b", 3]], {0: ["a", "b"], 1: [1, [2, 3]]}), + pytest.param( + [["a", 1], ["a", 2], ["b", 3], ["b", 3]], + {0: ["a", "b"], 1: [[1, 2], 3]}, + marks=pytest.mark.xfail, + ), ], ) def test_agg_of_mode_list(test, constant):