Skip to content

Commit

Permalink
chore: apply suggestions and handle polars exception separately
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroMiola committed Nov 6, 2024
1 parent a858860 commit ac7cd3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
7 changes: 1 addition & 6 deletions narwhals/_polars/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,9 @@ def mean_horizontal(self, *exprs: IntoPolarsExpr) -> PolarsExpr:
def median(self, *column_names: str) -> PolarsExpr:
import polars as pl # ignore-banned-import()

from narwhals._exceptions import InvalidOperationError
from narwhals._polars.expr import PolarsExpr

try:
return PolarsExpr(pl.median([*column_names]), dtypes=self._dtypes) # type: ignore[arg-type]
except pl.exceptions.InvalidOperationError as e:
msg = "`median` operation not supported for non-numeric input type."
raise InvalidOperationError(msg) from e
return PolarsExpr(pl.median([*column_names]), dtypes=self._dtypes) # type: ignore[arg-type]

def concat_str(
self,
Expand Down
21 changes: 13 additions & 8 deletions tests/expr_and_series/median_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,22 @@ def test_median_series(
def test_median_expr_raises_on_str(
constructor: Constructor,
expr: nw.Expr,
request: pytest.FixtureRequest,
) -> None:
if "polars" in str(constructor):
request.applymarker(pytest.mark.xfail)
from polars.exceptions import InvalidOperationError as PlInvalidOperationError

df = nw.from_native(constructor(data))
with pytest.raises(
InvalidOperationError,
match="`median` operation not supported for non-numeric input type.",
):
df.select(expr)
if "polars_lazy" in str(constructor):
with pytest.raises(
PlInvalidOperationError,
match="`median` operation not supported for dtype `str`",
):
df.select(expr).lazy().collect()
else:
with pytest.raises(
(InvalidOperationError, PlInvalidOperationError),
match="`median` operation not supported",
):
df.select(expr)


@pytest.mark.parametrize(("col"), [("s")])
Expand Down

0 comments on commit ac7cd3b

Please sign in to comment.