From 9cbbcd74387b5a64bcab9fb46d609f91315b0c64 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 25 Sep 2021 14:55:02 -0400 Subject: [PATCH 1/2] TST: GroupBy.aggregate with empty NDFrames with MultiIndex --- .../tests/groupby/aggregate/test_aggregate.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 4bda0e6ef9872..e83a1e9d634c4 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -17,6 +17,7 @@ from pandas import ( DataFrame, Index, + Int64Index, MultiIndex, Series, concat, @@ -861,6 +862,24 @@ def test_groupby_aggregate_empty_key_empty_return(): tm.assert_frame_equal(result, expected) +def test_groupby_aggregate_empty_with_multiindex_series(): + # GH 39178 + ser = Series(name="a", dtype=object) + result = ser.groupby([]).agg(list) + expected = Series(index=Int64Index([], dtype="int64"), name="a", dtype=object) + tm.assert_series_equal(result, expected) + + +def test_groupby_aggregate_empty_with_multiindex_frame(): + # GH 39178 + df = DataFrame(columns=["a", "b", "c"]) + result = df.groupby(["a", "b"]).agg(d=("c", list)) + expected = DataFrame( + columns=["d"], index=MultiIndex([[], []], [[], []], names=["a", "b"]) + ) + tm.assert_frame_equal(result, expected) + + def test_grouby_agg_loses_results_with_as_index_false_relabel(): # GH 32240: When the aggregate function relabels column names and # as_index=False is specified, the results are dropped. From 304e94d38d540b8fa3ea319b575d233f845cd836 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 25 Sep 2021 15:00:13 -0400 Subject: [PATCH 2/2] Remove non-multiindex test --- pandas/tests/groupby/aggregate/test_aggregate.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index e83a1e9d634c4..7bb850d38340f 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -17,7 +17,6 @@ from pandas import ( DataFrame, Index, - Int64Index, MultiIndex, Series, concat, @@ -862,14 +861,6 @@ def test_groupby_aggregate_empty_key_empty_return(): tm.assert_frame_equal(result, expected) -def test_groupby_aggregate_empty_with_multiindex_series(): - # GH 39178 - ser = Series(name="a", dtype=object) - result = ser.groupby([]).agg(list) - expected = Series(index=Int64Index([], dtype="int64"), name="a", dtype=object) - tm.assert_series_equal(result, expected) - - def test_groupby_aggregate_empty_with_multiindex_frame(): # GH 39178 df = DataFrame(columns=["a", "b", "c"])