From d2be88bb06e77eb1c092a49750072c12844a0e3b Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Mon, 14 Feb 2022 19:59:24 +0800 Subject: [PATCH 1/6] Add test --- pandas/tests/groupby/test_indexing.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index 0caa17f387a94..50ef8e8ba6068 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -314,3 +314,17 @@ def test_groupby_duplicated_columns(func): [[1.5, 3.0, 1.5]], columns=["A", "B", "A"], index=pd.Index(["G"], name="C") ) tm.assert_frame_equal(result, expected) + +def test_dataframe_groupby_incorrect_with_multiindex_make_df_from_data(): + # GH#32492 + df = pd.DataFrame( + data={ + "A": ["a1", "a2", None], + "B": ["b1", "b2", "b1"], + "val": [1, 2, 3], + } + ) + grps = df.groupby(by=["A", "B"]) + + with pytest.raises(KeyError): + grps.get_group(("a2", "b1")) From 3d64956233a1a967dfb4dc2385209a8164cf50cd Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Mon, 14 Feb 2022 22:31:01 +0800 Subject: [PATCH 2/6] Update test_indexing.py --- pandas/tests/groupby/test_indexing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index 50ef8e8ba6068..a2c9be2190af0 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -314,7 +314,8 @@ def test_groupby_duplicated_columns(func): [[1.5, 3.0, 1.5]], columns=["A", "B", "A"], index=pd.Index(["G"], name="C") ) tm.assert_frame_equal(result, expected) - + + def test_dataframe_groupby_incorrect_with_multiindex_make_df_from_data(): # GH#32492 df = pd.DataFrame( From e0a1e1ed1f187100b1ea05c39f21a9961dc08da0 Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Tue, 15 Feb 2022 00:41:29 +0800 Subject: [PATCH 3/6] Update test_indexing.py --- pandas/tests/groupby/test_indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index a2c9be2190af0..bcf830e9cc4b7 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -327,5 +327,5 @@ def test_dataframe_groupby_incorrect_with_multiindex_make_df_from_data(): ) grps = df.groupby(by=["A", "B"]) - with pytest.raises(KeyError): + with pytest.raises(match=KeyError): grps.get_group(("a2", "b1")) From 839819daee8c2e4a2002476b00cf8d73ffb91de8 Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Tue, 15 Feb 2022 13:47:32 +0800 Subject: [PATCH 4/6] Update pytest.raises --- pandas/tests/groupby/test_indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index bcf830e9cc4b7..983355c6cfff4 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -327,5 +327,5 @@ def test_dataframe_groupby_incorrect_with_multiindex_make_df_from_data(): ) grps = df.groupby(by=["A", "B"]) - with pytest.raises(match=KeyError): + with pytest.raises(KeyError, match="Selecting group which is not exist"): grps.get_group(("a2", "b1")) From edefa16f089eb38f6b993e967cb6cb740144546d Mon Sep 17 00:00:00 2001 From: "chean.wei.khor" Date: Tue, 15 Feb 2022 19:59:08 +0800 Subject: [PATCH 5/6] add match --- pandas/tests/groupby/test_indexing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index 983355c6cfff4..9c1aded037ebe 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -327,5 +327,6 @@ def test_dataframe_groupby_incorrect_with_multiindex_make_df_from_data(): ) grps = df.groupby(by=["A", "B"]) - with pytest.raises(KeyError, match="Selecting group which is not exist"): + msg = "('a2', 'b1')" + with pytest.raises(KeyError, match=msg): grps.get_group(("a2", "b1")) From 9411da80405f3b730aa60d78cb8cf1d131a4df5c Mon Sep 17 00:00:00 2001 From: "chean.wei.khor" Date: Tue, 15 Feb 2022 20:02:46 +0800 Subject: [PATCH 6/6] change function --- pandas/tests/groupby/test_indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py index 9c1aded037ebe..35236c3afc942 100644 --- a/pandas/tests/groupby/test_indexing.py +++ b/pandas/tests/groupby/test_indexing.py @@ -316,7 +316,7 @@ def test_groupby_duplicated_columns(func): tm.assert_frame_equal(result, expected) -def test_dataframe_groupby_incorrect_with_multiindex_make_df_from_data(): +def test_groupby_get_nonexisting_groups(): # GH#32492 df = pd.DataFrame( data={