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

feat: add DaskExpr.quantile #835

Merged
merged 4 commits into from
Aug 22, 2024
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
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
Loading