Skip to content

Commit 600b9c8

Browse files
raguiar2TomAugspurger
authored andcommitted
Raised value error on incorrect na_option (#22037)
* added value error on incorrect na_option * added test cases
1 parent a6c7387 commit 600b9c8

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -7513,6 +7513,10 @@ def rank(self, axis=0, method='average', numeric_only=None,
75137513
msg = "rank does not make sense when ndim > 2"
75147514
raise NotImplementedError(msg)
75157515

7516+
if na_option not in {'keep', 'top', 'bottom'}:
7517+
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
7518+
raise ValueError(msg)
7519+
75167520
def ranker(data):
75177521
ranks = algos.rank(data.values, axis=axis, method=method,
75187522
ascending=ascending, na_option=na_option,

pandas/tests/frame/test_rank.py

+10
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ def test_rank_na_option(self):
191191
tm.assert_numpy_array_equal(ranks0.values, exp0)
192192
tm.assert_numpy_array_equal(ranks1.values, exp1)
193193

194+
# bad values throw error
195+
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
196+
197+
with tm.assert_raises_regex(ValueError, msg):
198+
self.frame.rank(na_option='bad', ascending=False)
199+
200+
# invalid type
201+
with tm.assert_raises_regex(ValueError, msg):
202+
self.frame.rank(na_option=True, ascending=False)
203+
194204
def test_rank_axis(self):
195205
# check if using axes' names gives the same result
196206
df = DataFrame([[2, 1], [4, 3]])

pandas/tests/groupby/test_rank.py

+11
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ def test_rank_avg_even_vals():
248248
def test_rank_object_raises(ties_method, ascending, na_option,
249249
pct, vals):
250250
df = DataFrame({'key': ['foo'] * 5, 'val': vals})
251+
252+
with tm.assert_raises_regex(TypeError, "not callable"):
253+
df.groupby('key').rank(method=ties_method,
254+
ascending=ascending,
255+
na_option='bad', pct=pct)
256+
257+
with tm.assert_raises_regex(TypeError, "not callable"):
258+
df.groupby('key').rank(method=ties_method,
259+
ascending=ascending,
260+
na_option=True, pct=pct)
261+
251262
with tm.assert_raises_regex(TypeError, "not callable"):
252263
df.groupby('key').rank(method=ties_method,
253264
ascending=ascending,

pandas/tests/series/test_rank.py

+10
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ def test_rank_categorical(self):
183183
exp_keep
184184
)
185185

186+
# Test invalid values for na_option
187+
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
188+
189+
with tm.assert_raises_regex(ValueError, msg):
190+
na_ser.rank(na_option='bad', ascending=False)
191+
192+
# invalid type
193+
with tm.assert_raises_regex(ValueError, msg):
194+
na_ser.rank(na_option=True, ascending=False)
195+
186196
# Test with pct=True
187197
na_ser = Series(['first', 'second', 'third', 'fourth', np.NaN]).astype(
188198
CategoricalDtype(['first', 'second', 'third', 'fourth'], True))

0 commit comments

Comments
 (0)