Skip to content

Commit 41489aa

Browse files
authored
feat: dask expr len method (#762)
1 parent 813db11 commit 41489aa

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

narwhals/_dask/expr.py

+7
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ def clip(
473473
returns_scalar=False,
474474
)
475475

476+
def len(self: Self) -> Self:
477+
return self._from_call(
478+
lambda _input: _input.size,
479+
"len",
480+
returns_scalar=True,
481+
)
482+
476483
@property
477484
def str(self: Self) -> DaskExprStringNamespace:
478485
return DaskExprStringNamespace(self)

tests/expr_and_series/len_test.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
from tests.utils import compare_dicts
77

88

9-
def test_len(constructor: Any, request: Any) -> None:
9+
def test_len_no_filter(constructor: Any) -> None:
10+
data = {"a": list("xyz"), "b": [1, 2, 1]}
11+
expected = {"l": [3], "l2": [6]}
12+
df = nw.from_native(constructor(data)).select(
13+
nw.col("a").len().alias("l"),
14+
(nw.col("a").len() * 2).alias("l2"),
15+
)
16+
17+
compare_dicts(df, expected)
18+
19+
20+
def test_len_len_chaining(constructor: Any, request: Any) -> None:
1021
data = {"a": list("xyz"), "b": [1, 2, 1]}
1122
expected = {"a1": [2], "a2": [1]}
1223
if "dask" in str(constructor):

tests/test_group_by.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def test_group_by_iter(constructor_eager: Any) -> None:
9494
assert sorted(keys) == sorted(expected_keys)
9595

9696

97-
def test_group_by_len(constructor: Any, request: Any) -> None:
98-
if "dask" in str(constructor):
99-
request.applymarker(pytest.mark.xfail)
97+
def test_group_by_len(constructor: Any) -> None:
10098
result = (
10199
nw.from_native(constructor(data)).group_by("a").agg(nw.col("b").len()).sort("a")
102100
)

0 commit comments

Comments
 (0)