Skip to content

Commit

Permalink
feat: add DaskExpr.quantile (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
anopsy authored Aug 22, 2024
1 parent 18d0dcb commit 144f572
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/expr_and_series/quantile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down

0 comments on commit 144f572

Please sign in to comment.