diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index ec7f3200b682b..8a98b27486d90 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1171,6 +1171,38 @@ def test_groupby_sum_below_mincount_nullable_integer(): tm.assert_frame_equal(result, expected) +def test_if_empty_multiindex(): + # GH 32464 + # Test if index after groupby with more then one column is always MultiIndex + a = DataFrame({"a": [1, 2], "b": [5, 6], "c": [8, 9]}) + + agg_1 = a.groupby(["a", "b"]).sum() + agg_2 = a.groupby(["a", "b", "c"]).sum() + + expected = MultiIndex( + levels=[[1, 2], [5, 6]], codes=[[0, 1], [0, 1]], names=["a", "b"] + ) + result = agg_1.index + + tm.assert_index_equal(expected, result) + + expected = MultiIndex( + levels=[[1, 2], [5, 6], [8, 9]], + codes=[[0, 1], [0, 1], [0, 1]], + names=["a", "b", "c"], + ) + result = agg_2.index + + # Tests if group by with all columns has a MultiIndex + tm.assert_index_equal(expected, result) + + index_1 = agg_1.iloc[:, :0].index + index_2 = agg_2.droplevel("c").index + + # Tests if both agreggations have multiindex + tm.assert_index_equal(index_1, index_2) + + def test_mean_on_timedelta(): # GH 17382 df = DataFrame({"time": pd.to_timedelta(range(10)), "cat": ["A", "B"] * 5})