From ad5653827fef135cac0343a69e5155a47c16fb06 Mon Sep 17 00:00:00 2001 From: Charlie Phillips Date: Sun, 6 Oct 2019 18:03:23 -0400 Subject: [PATCH 1/2] TST: Dataframe KeyError when getting nonexistant category (#28799) --- pandas/tests/frame/test_indexing.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index 6d239e96cd167..fc6e25d715b55 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3856,3 +3856,15 @@ def test_functions_no_warnings(self): df["group"] = pd.cut( df.value, range(0, 105, 10), right=False, labels=labels ) + + def test_get_nonexistant_category(self): + # Accessing a Category that is not in the dataframe + df = pd.DataFrame({"var": ["a", "a", "b", "b"], "val": range(4)}) + with pytest.raises(KeyError) as col_error: + df.groupby("var").apply( + lambda rows: pd.DataFrame( + {"var": [rows.iloc[-1]["var"]], "val": [rows.iloc[-1]["vau"]]} + ) + ) + # Correct Category is thrown with exception + assert str(col_error.value) == "'vau'" From 4f21cd185a09fa6a05cf39a668b9f6465d304ec1 Mon Sep 17 00:00:00 2001 From: Charlie Phillips Date: Sun, 6 Oct 2019 22:27:07 -0400 Subject: [PATCH 2/2] TST: Changed location and fixed typo for test resolving (#28799) --- pandas/tests/frame/test_indexing.py | 12 ------------ pandas/tests/groupby/test_categorical.py | 11 +++++++++++ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index fc6e25d715b55..6d239e96cd167 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3856,15 +3856,3 @@ def test_functions_no_warnings(self): df["group"] = pd.cut( df.value, range(0, 105, 10), right=False, labels=labels ) - - def test_get_nonexistant_category(self): - # Accessing a Category that is not in the dataframe - df = pd.DataFrame({"var": ["a", "a", "b", "b"], "val": range(4)}) - with pytest.raises(KeyError) as col_error: - df.groupby("var").apply( - lambda rows: pd.DataFrame( - {"var": [rows.iloc[-1]["var"]], "val": [rows.iloc[-1]["vau"]]} - ) - ) - # Correct Category is thrown with exception - assert str(col_error.value) == "'vau'" diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index fcc0aa3b1c015..3389db6cf7095 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1193,3 +1193,14 @@ def test_groupby_categorical_axis_1(code): result = df.groupby(cat, axis=1).mean() expected = df.T.groupby(cat, axis=0).mean().T assert_frame_equal(result, expected) + + +def test_get_nonexistent_category(): + # Accessing a Category that is not in the dataframe + df = pd.DataFrame({"var": ["a", "a", "b", "b"], "val": range(4)}) + with pytest.raises(KeyError, match="'vau'"): + df.groupby("var").apply( + lambda rows: pd.DataFrame( + {"var": [rows.iloc[-1]["var"]], "val": [rows.iloc[-1]["vau"]]} + ) + )