Skip to content

Commit

Permalink
feat: added dask Expr.__sub__ (#643)
Browse files Browse the repository at this point in the history
* dask dunder sub

* added sub test cases in test_with_columns

* resolving
  • Loading branch information
anopsy authored Jul 27, 2024
1 parent a3d19f4 commit a9c34dd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ def __add__(self, other: Any) -> Self:
other,
)

def __sub__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__sub__(other),
"__sub__",
other,
)

def __mul__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__mul__(other),
Expand Down
13 changes: 12 additions & 1 deletion narwhals/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import sys
import warnings
from typing import TYPE_CHECKING
from typing import Any

Expand Down Expand Up @@ -72,7 +73,17 @@ def get_dask() -> Any:

def get_dask_dataframe() -> Any:
"""Get dask.dataframe module (if already imported - else return None)."""
return sys.modules.get("dask.dataframe", None)
if "dask" in sys.modules:
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="\nDask dataframe query planning.*",
category=FutureWarning,
)
import dask.dataframe as dd

return dd
return None # pragma: no cover


def get_dask_expr() -> Any:
Expand Down
4 changes: 4 additions & 0 deletions tests/dask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def test_with_columns() -> None:
(nw.col("a") + nw.col("b").mean()).alias("c"),
d=nw.col("a"),
e=nw.col("a") + nw.col("b"),
f=nw.col("b") - 1,
g=nw.col("a") - nw.col("b"),
h=nw.col("a") * 3,
i=nw.col("a") * nw.col("b"),
)
Expand All @@ -55,6 +57,8 @@ def test_with_columns() -> None:
"c": [6.0, 7.0, 8.0],
"d": [1, 2, 3],
"e": [5, 7, 9],
"f": [3, 4, 5],
"g": [-3, -3, -3],
"h": [3, 6, 9],
"i": [4, 10, 18],
},
Expand Down
4 changes: 2 additions & 2 deletions tests/expr_and_series/gather_every_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_gather_every_expr(constructor_with_lazy: Any, n: int, offset: int) -> N

@pytest.mark.parametrize("n", [1, 2, 3])
@pytest.mark.parametrize("offset", [1, 2, 3])
def test_gather_every_series(constructor: Any, n: int, offset: int) -> None:
series = nw.from_native(constructor(data), eager_only=True)["a"]
def test_gather_every_series(constructor_series: Any, n: int, offset: int) -> None:
series = nw.from_native(constructor_series(data["a"]), series_only=True)

result = series.gather_every(n=n, offset=offset)
expected = data["a"][offset::n]
Expand Down

0 comments on commit a9c34dd

Please sign in to comment.