diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 38f334762fa88..104f7e0550f60 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7480,6 +7480,10 @@ def rank(self, axis=0, method='average', numeric_only=None, msg = "rank does not make sense when ndim > 2" raise NotImplementedError(msg) + if na_option not in {'keep', 'top', 'bottom'}: + msg = "na_option must be one of 'keep', 'top', or 'bottom'" + raise ValueError(msg) + def ranker(data): ranks = algos.rank(data.values, axis=axis, method=method, ascending=ascending, na_option=na_option, diff --git a/pandas/tests/frame/test_rank.py b/pandas/tests/frame/test_rank.py index a1210f1ed54e4..c5d771f52b6ac 100644 --- a/pandas/tests/frame/test_rank.py +++ b/pandas/tests/frame/test_rank.py @@ -191,6 +191,16 @@ def test_rank_na_option(self): tm.assert_numpy_array_equal(ranks0.values, exp0) tm.assert_numpy_array_equal(ranks1.values, exp1) + # bad values throw error + msg = "na_option must be one of 'keep', 'top', or 'bottom'" + + with tm.assert_raises_regex(ValueError, msg): + self.frame.rank(na_option='bad', ascending=False) + + # invalid type + with tm.assert_raises_regex(ValueError, msg): + self.frame.rank(na_option=True, ascending=False) + def test_rank_axis(self): # check if using axes' names gives the same result df = DataFrame([[2, 1], [4, 3]]) diff --git a/pandas/tests/groupby/test_rank.py b/pandas/tests/groupby/test_rank.py index 203c3c73bec94..0628f9c79a154 100644 --- a/pandas/tests/groupby/test_rank.py +++ b/pandas/tests/groupby/test_rank.py @@ -248,6 +248,17 @@ def test_rank_avg_even_vals(): def test_rank_object_raises(ties_method, ascending, na_option, pct, vals): df = DataFrame({'key': ['foo'] * 5, 'val': vals}) + + with tm.assert_raises_regex(TypeError, "not callable"): + df.groupby('key').rank(method=ties_method, + ascending=ascending, + na_option='bad', pct=pct) + + with tm.assert_raises_regex(TypeError, "not callable"): + df.groupby('key').rank(method=ties_method, + ascending=ascending, + na_option=True, pct=pct) + with tm.assert_raises_regex(TypeError, "not callable"): df.groupby('key').rank(method=ties_method, ascending=ascending, diff --git a/pandas/tests/series/test_rank.py b/pandas/tests/series/test_rank.py index 004e42e14cb93..d0e001cbfcd88 100644 --- a/pandas/tests/series/test_rank.py +++ b/pandas/tests/series/test_rank.py @@ -183,6 +183,16 @@ def test_rank_categorical(self): exp_keep ) + # Test invalid values for na_option + msg = "na_option must be one of 'keep', 'top', or 'bottom'" + + with tm.assert_raises_regex(ValueError, msg): + na_ser.rank(na_option='bad', ascending=False) + + # invalid type + with tm.assert_raises_regex(ValueError, msg): + na_ser.rank(na_option=True, ascending=False) + # Test with pct=True na_ser = Series(['first', 'second', 'third', 'fourth', np.NaN]).astype( CategoricalDtype(['first', 'second', 'third', 'fourth'], True))