Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raised value error on incorrect na_option #22037

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down