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: dask expr __invert__, is_null, n_unique methods #763

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ def __ror__(self, other: DaskExpr) -> Self: # pragma: no cover
returns_scalar=False,
)

def __invert__(self: Self) -> Self:
return self._from_call(
lambda _input: _input.__invert__(),
"__invert__",
returns_scalar=False,
)

def mean(self) -> Self:
return self._from_call(
lambda _input: _input.mean(),
Expand Down Expand Up @@ -473,6 +480,20 @@ def clip(
returns_scalar=False,
)

def n_unique(self: Self) -> Self:
return self._from_call(
lambda _input: _input.nunique(dropna=False),
"n_unique",
returns_scalar=True,
)

def is_null(self: Self) -> Self:
return self._from_call(
lambda _input: _input.isna(),
"is_null",
returns_scalar=False,
)

@property
def str(self: Self) -> DaskExprStringNamespace:
return DaskExprStringNamespace(self)
Expand Down
22 changes: 22 additions & 0 deletions tests/expr_and_series/is_null_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Any

import narwhals.stable.v1 as nw
from tests.utils import compare_dicts


def test_null(constructor: Any) -> None:
data_na = {"a": [None, 3, 2], "z": [7.0, None, None]}
expected = {"a": [True, False, False], "z": [True, False, False]}
df = nw.from_native(constructor(data_na))
result = df.select(nw.col("a").is_null(), ~nw.col("z").is_null())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, this is the only place in which we also test __invert__, that's why the implementation ended up in the same pr


compare_dicts(result, expected)


def test_null_series(constructor_eager: Any) -> None:
data_na = {"a": [None, 3, 2], "z": [7.0, None, None]}
expected = {"a": [True, False, False], "z": [True, False, False]}
df = nw.from_native(constructor_eager(data_na), eager_only=True)
result = {"a": df["a"].is_null(), "z": ~df["z"].is_null()}

compare_dicts(result, expected)
6 changes: 1 addition & 5 deletions tests/expr_and_series/n_unique_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Any

import pytest

import narwhals.stable.v1 as nw
from tests.utils import compare_dicts

Expand All @@ -11,9 +9,7 @@
}


def test_n_unique(constructor: Any, request: Any) -> None:
if "dask" in str(constructor):
request.applymarker(pytest.mark.xfail)
def test_n_unique(constructor: Any) -> None:
df = nw.from_native(constructor(data))
result = df.select(nw.all().n_unique())
expected = {"a": [3], "b": [4]}
Expand Down
16 changes: 0 additions & 16 deletions tests/expr_and_series/null_test.py

This file was deleted.