Skip to content

Commit

Permalink
merge main, test invalid method
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Nov 10, 2024
1 parent cafed4b commit 4c8cc1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,10 @@ def rank(

supported_rank_methods = {"average", "min", "max", "dense", "ordinal"}
if method not in supported_rank_methods:
msg = f"Ranking method must be one of {supported_rank_methods}. Found '{method}'"
msg = (
"Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. "
f"Found '{method}'"
)
raise ValueError(msg)

return self.__class__(
Expand Down
5 changes: 4 additions & 1 deletion narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,10 @@ def rank(
"""
supported_rank_methods = {"average", "min", "max", "dense", "ordinal"}
if method not in supported_rank_methods:
msg = f"Ranking method must be one of {supported_rank_methods}. Found '{method}'"
msg = (
"Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. "
f"Found '{method}'"
)
raise ValueError(msg)

return self._from_compliant_series(
Expand Down
16 changes: 16 additions & 0 deletions tests/expr_and_series/rank_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,19 @@ def test_rank_expr_in_over_context(
result = df.select(nw.col("a").rank(method=method).over("b"))
expected_data = {"a": expected_over[method]}
assert_equal_data(result, expected_data)


def test_invalid_method_raise(constructor: Constructor) -> None:
method = "invalid_method_name"
df = nw.from_native(constructor(data_float))

msg = (
"Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. "
f"Found '{method}'"
)

with pytest.raises(ValueError, match=msg):
df.select(nw.col("a").rank(method=method)) # type: ignore[arg-type]

with pytest.raises(ValueError, match=msg):
df.lazy().collect()["a"].rank(method=method) # type: ignore[arg-type]

0 comments on commit 4c8cc1b

Please sign in to comment.