diff --git a/narwhals/_dask/expr.py b/narwhals/_dask/expr.py index faedb6095..84288cf53 100644 --- a/narwhals/_dask/expr.py +++ b/narwhals/_dask/expr.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING from typing import Any from typing import Callable +from typing import Literal from typing import NoReturn from narwhals._dask.utils import add_row_index @@ -515,6 +516,22 @@ def len(self: Self) -> Self: returns_scalar=True, ) + def quantile( + self: Self, + quantile: float, + interpolation: Literal["nearest", "higher", "lower", "midpoint", "linear"], + ) -> Self: + if interpolation == "linear": + return self._from_call( + lambda _input, quantile: _input.quantile(q=quantile, method="dask"), + "quantile", + quantile, + returns_scalar=True, + ) + else: + msg = "`higher`, `lower`, `midpoint`, `nearest` - interpolation methods are not supported by Dask. Please use `linear` instead." + raise NotImplementedError(msg) + def is_first_distinct(self: Self) -> Self: def func(_input: Any) -> Any: _name = _input.name diff --git a/narwhals/expr.py b/narwhals/expr.py index a8097a2a2..cabdfb4d1 100644 --- a/narwhals/expr.py +++ b/narwhals/expr.py @@ -1537,7 +1537,9 @@ def quantile( r"""Get quantile value. Note: - pandas and Polars may have implementation differences for a given interpolation method. + * pandas and Polars may have implementation differences for a given interpolation method. + * [dask](https://docs.dask.org/en/stable/generated/dask.dataframe.Series.quantile.html) has its own method to approximate quantile and it doesn't implement 'nearest', 'higher', 'lower', 'midpoint' + as interpolation method - use 'linear' which is closest to the native 'dask' - method. Arguments: quantile : float diff --git a/tests/expr_and_series/quantile_test.py b/tests/expr_and_series/quantile_test.py index 8cb9320c7..d9064541f 100644 --- a/tests/expr_and_series/quantile_test.py +++ b/tests/expr_and_series/quantile_test.py @@ -26,7 +26,7 @@ def test_quantile_expr( expected: dict[str, list[float]], request: Any, ) -> None: - if "dask" in str(constructor): + if "dask" in str(constructor) and interpolation != "linear": request.applymarker(pytest.mark.xfail) q = 0.3 data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}