From daad91df6e7daa4ea4aac4ff76524b17e963cc2f Mon Sep 17 00:00:00 2001 From: Maciej Jarosiewicz Date: Sat, 2 Nov 2019 15:17:48 +0000 Subject: [PATCH] ensure consistent structure for groupby on index and column --- pandas/tests/groupby/test_grouping.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index e1fd8d7da6833..979e7b2dd7ffc 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -14,6 +14,7 @@ date_range, ) from pandas.core.groupby.grouper import Grouping +from pandas.core.indexes.frozen import FrozenList import pandas.util.testing as tm # selection @@ -641,6 +642,27 @@ def test_groupby_level_index_value_all_na(self): ) tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize( + "df", + [ + pd.DataFrame([[1, 2, 3]], columns=["a", "b", "c"]).set_index("a"), + pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"]).set_index( + "a" + ), + pd.DataFrame( + [[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=["a", "b", "c"] + ).set_index("a"), + ], + ) + @pytest.mark.parametrize( + "method", ["mean", "median", "prod", "min", "max", "sum", "std", "var"] + ) + def test_groupby_on_index_and_column_consistent_structure(self, df, method): + # https://github.com/pandas-dev/pandas/issues/17681 + df_gb = df.groupby(["a", "c"]) + result = getattr(df_gb, method)() + assert result.index.names == FrozenList(["a", "c"]) + # get_group # --------------------------------