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 len method #762

Merged
merged 1 commit into from
Aug 9, 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
7 changes: 7 additions & 0 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ def clip(
returns_scalar=False,
)

def len(self: Self) -> Self:
return self._from_call(
lambda _input: _input.size,
"len",
returns_scalar=True,
)

@property
def str(self: Self) -> DaskExprStringNamespace:
return DaskExprStringNamespace(self)
Expand Down
13 changes: 12 additions & 1 deletion tests/expr_and_series/len_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
from tests.utils import compare_dicts


def test_len(constructor: Any, request: Any) -> None:
def test_len_no_filter(constructor: Any) -> None:
data = {"a": list("xyz"), "b": [1, 2, 1]}
expected = {"l": [3], "l2": [6]}
df = nw.from_native(constructor(data)).select(
nw.col("a").len().alias("l"),
(nw.col("a").len() * 2).alias("l2"),
)

compare_dicts(df, expected)


def test_len_len_chaining(constructor: Any, request: Any) -> None:
Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed this test, and keep failing for dask as .filter is not implemented. Wondering if that's ok to implement even in the sad case that we do not move forward with #743 .

At the end, I think expr.filter is mainly use to filter within a context and then aggregate (exactly as in the test here)

data = {"a": list("xyz"), "b": [1, 2, 1]}
expected = {"a1": [2], "a2": [1]}
if "dask" in str(constructor):
Expand Down
4 changes: 1 addition & 3 deletions tests/test_group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def test_group_by_iter(constructor_eager: Any) -> None:
assert sorted(keys) == sorted(expected_keys)


def test_group_by_len(constructor: Any, request: Any) -> None:
if "dask" in str(constructor):
request.applymarker(pytest.mark.xfail)
def test_group_by_len(constructor: Any) -> None:
result = (
nw.from_native(constructor(data)).group_by("a").agg(nw.col("b").len()).sort("a")
)
Expand Down
Loading