From 239d42a863befe34d8f414257a831b44dcd35019 Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Sat, 16 Oct 2021 00:02:18 -0300 Subject: [PATCH 1/7] added test to indexing on groupby, #32464 --- pandas/tests/groupby/test_indexing.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index b9f71fd4ed96a..5f0166a1118e2 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -285,3 +285,13 @@ def test_column_axis(column_group_df): expected = column_group_df.iloc[:, [1, 3]] tm.assert_frame_equal(result, expected) + + +def test_if_is_multiindex(): + a = pd.DataFrame({"a": [], "b": [], "c": []}) + + index_1 = a.groupby(["a", "b"]).sum().index + index_2 = a.groupby(["a", "b", "c"]).sum().index + + assert isinstance(index_2, pd.core.indexes.multi.MultiIndex) + assert type(index_1) == type(index_2) From b42f6e5a7b84ff3a2fc57b6cdf9e95f489a4455d Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Sat, 16 Oct 2021 00:10:31 -0300 Subject: [PATCH 2/7] added comment --- pandas/tests/groupby/test_indexing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index 5f0166a1118e2..a28736e58d62d 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -288,6 +288,8 @@ def test_column_axis(column_group_df): def test_if_is_multiindex(): + # GH 32464 + # Test if index after groupby with more then one column is always MultiIndex a = pd.DataFrame({"a": [], "b": [], "c": []}) index_1 = a.groupby(["a", "b"]).sum().index From 57f86c382bbaa5182c28ca5bd2d7af14ed611a40 Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Sun, 17 Oct 2021 10:36:47 -0300 Subject: [PATCH 3/7] changed file and asserting method --- pandas/tests/groupby/test_function.py | 21 +++++++++++++++++++++ pandas/tests/groupby/test_indexing.py | 12 ------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 3ae11847cc06b..de70de1beff48 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1155,3 +1155,24 @@ def test_groupby_sum_below_mincount_nullable_integer(): result = grouped.sum(min_count=2) expected = DataFrame({"b": [pd.NA] * 3, "c": [pd.NA] * 3}, dtype="Int64", index=idx) tm.assert_frame_equal(result, expected) + + +def test_if_is_multiindex(): + # GH 32464 + # Test if index after groupby with more then one column is always MultiIndex + a = DataFrame({"a": [], "b": [], "c": []}) + result = a.groupby(["a", "b", "c"]).sum() + + expected_index = MultiIndex.from_arrays([[], [], []], names=("a", "b", "c")) + expected = DataFrame( + np.ndarray((0, 0)), dtype="float64", index=expected_index, columns=Index([]) + ) + + # Tests if groupby with all columns has a multiindex + tm.assert_frame_equal(result, expected) + + agg_1 = a.groupby(["a", "b"]).sum().iloc[:, :0] + agg_2 = a.groupby(["a", "b", "c"]).sum().droplevel("c") + + # Tests if both agreggations have multiindex + tm.assert_frame_equal(agg_1, agg_2) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index a28736e58d62d..b9f71fd4ed96a 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -285,15 +285,3 @@ def test_column_axis(column_group_df): expected = column_group_df.iloc[:, [1, 3]] tm.assert_frame_equal(result, expected) - - -def test_if_is_multiindex(): - # GH 32464 - # Test if index after groupby with more then one column is always MultiIndex - a = pd.DataFrame({"a": [], "b": [], "c": []}) - - index_1 = a.groupby(["a", "b"]).sum().index - index_2 = a.groupby(["a", "b", "c"]).sum().index - - assert isinstance(index_2, pd.core.indexes.multi.MultiIndex) - assert type(index_1) == type(index_2) From c89f4ad3a7885a4135936b211eebcf7f6df4a9d6 Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Sun, 17 Oct 2021 10:41:55 -0300 Subject: [PATCH 4/7] removed test to see if df has multiindex --- pandas/tests/groupby/test_function.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index de70de1beff48..4337ae9f23fbf 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1161,15 +1161,6 @@ def test_if_is_multiindex(): # GH 32464 # Test if index after groupby with more then one column is always MultiIndex a = DataFrame({"a": [], "b": [], "c": []}) - result = a.groupby(["a", "b", "c"]).sum() - - expected_index = MultiIndex.from_arrays([[], [], []], names=("a", "b", "c")) - expected = DataFrame( - np.ndarray((0, 0)), dtype="float64", index=expected_index, columns=Index([]) - ) - - # Tests if groupby with all columns has a multiindex - tm.assert_frame_equal(result, expected) agg_1 = a.groupby(["a", "b"]).sum().iloc[:, :0] agg_2 = a.groupby(["a", "b", "c"]).sum().droplevel("c") From 4bf0563706e80208804903d17883be143da28286 Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Sun, 17 Oct 2021 19:03:50 -0300 Subject: [PATCH 5/7] changed assert method --- pandas/tests/groupby/test_function.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 4337ae9f23fbf..d12f80f2e7c3b 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1162,8 +1162,14 @@ def test_if_is_multiindex(): # Test if index after groupby with more then one column is always MultiIndex a = DataFrame({"a": [], "b": [], "c": []}) - agg_1 = a.groupby(["a", "b"]).sum().iloc[:, :0] - agg_2 = a.groupby(["a", "b", "c"]).sum().droplevel("c") + agg_1 = a.groupby(["a", "b"]).sum() + agg_2 = a.groupby(["a", "b", "c"]).sum() + + # Tests if group by with all columns has a MultiIndex + assert isinstance(agg_2.index, pd.core.indexes.multi.MultiIndex) + + index_1 = agg_1.iloc[:, :0].index + index_2 = agg_2.droplevel("c").index # Tests if both agreggations have multiindex - tm.assert_frame_equal(agg_1, agg_2) + tm.assert_index_equal(index_1, index_2) From 9ee9c8e1b834b73567befda958d10dc02fe848bd Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Tue, 16 Nov 2021 21:33:41 -0300 Subject: [PATCH 6/7] using multiindex constructor --- pandas/tests/groupby/test_function.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 19194a14f6ec1..93413a0b9c8ca 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1157,13 +1157,27 @@ def test_groupby_sum_below_mincount_nullable_integer(): def test_if_is_multiindex(): # GH 32464 # Test if index after groupby with more then one column is always MultiIndex - a = DataFrame({"a": [], "b": [], "c": []}) + 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 - assert isinstance(agg_2.index, pd.core.indexes.multi.MultiIndex) + tm.assert_index_equal(expected, result) index_1 = agg_1.iloc[:, :0].index index_2 = agg_2.droplevel("c").index From de65783c6735defd081476ae713c710a55b99058 Mon Sep 17 00:00:00 2001 From: Gabriel Di Pardi Arruda Date: Tue, 22 Feb 2022 20:23:22 -0300 Subject: [PATCH 7/7] changed test name --- pandas/tests/groupby/test_function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index e0d3cb46b1a05..261833542a3df 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1167,7 +1167,7 @@ def test_groupby_sum_below_mincount_nullable_integer(): tm.assert_frame_equal(result, expected) -def test_if_is_multiindex(): +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]})