Skip to content

Commit 19a7f9d

Browse files
bravechproost
authored andcommitted
TST: Dataframe KeyError when getting nonexistent category (pandas-dev#28799) (pandas-dev#28809)
1 parent 248ee58 commit 19a7f9d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

pandas/tests/groupby/test_categorical.py

+11
Original file line numberDiff line numberDiff line change
@@ -1210,3 +1210,14 @@ def test_groupby_cat_preserves_structure(observed, ordered_fixture):
12101210
)
12111211

12121212
assert_frame_equal(result, expected)
1213+
1214+
1215+
def test_get_nonexistent_category():
1216+
# Accessing a Category that is not in the dataframe
1217+
df = pd.DataFrame({"var": ["a", "a", "b", "b"], "val": range(4)})
1218+
with pytest.raises(KeyError, match="'vau'"):
1219+
df.groupby("var").apply(
1220+
lambda rows: pd.DataFrame(
1221+
{"var": [rows.iloc[-1]["var"]], "val": [rows.iloc[-1]["vau"]]}
1222+
)
1223+
)

0 commit comments

Comments
 (0)