From d331b9915477ffecf8c5b02994c534a9ab5b300a Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 30 Jan 2023 12:33:05 -0800 Subject: [PATCH] test for non-ordered Categorical rank --- pandas/tests/groupby/test_rank.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/groupby/test_rank.py b/pandas/tests/groupby/test_rank.py index 8bbe38d3379ac..d0b848a567346 100644 --- a/pandas/tests/groupby/test_rank.py +++ b/pandas/tests/groupby/test_rank.py @@ -13,6 +13,23 @@ import pandas._testing as tm +def test_rank_unordered_categorical_typeerror(): + # GH#51034 should be TypeError, not NotImplementedError + cat = pd.Categorical([], ordered=False) + ser = Series(cat) + df = ser.to_frame() + + msg = "Cannot perform rank with non-ordered Categorical" + + gb = ser.groupby(cat) + with pytest.raises(TypeError, match=msg): + gb.rank() + + gb2 = df.groupby(cat) + with pytest.raises(TypeError, match=msg): + gb2.rank() + + def test_rank_apply(): lev1 = tm.rands_array(10, 100) lev2 = tm.rands_array(10, 130)