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 ends_with, contains, slice for dask #664

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
22 changes: 22 additions & 0 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,25 @@ def starts_with(self, prefix: str) -> DaskExpr:
return self._expr._from_call(
lambda _input, prefix: _input.str.startswith(prefix), "starts_with", prefix
)

def ends_with(self, suffix: str) -> DaskExpr:
return self._expr._from_call(
lambda _input, suffix: _input.str.endswith(suffix), "ends_with", suffix
)

def contains(self, pattern: str, *, literal: bool = False) -> DaskExpr:
return self._expr._from_call(
lambda _input, pat, regex: _input.str.contains(pat=pat, regex=regex),
"contains",
pattern,
not literal,
)

def slice(self, offset: int, length: int | None = None) -> DaskExpr:
stop = offset + length if length else None
return self._expr._from_call(
lambda _input, start, stop: _input.str.slice(start=start, stop=stop),
"slice",
offset,
stop,
)
52 changes: 52 additions & 0 deletions tests/dask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import sys
import warnings
from typing import Any

import pandas as pd
import pytest
Expand Down Expand Up @@ -123,3 +124,54 @@ def test_starts_with(prefix: str, expected: dict[str, list[bool]]) -> None:
result = df.with_columns(nw.col("a").str.starts_with(prefix))

compare_dicts(result, expected)


@pytest.mark.parametrize(
("suffix", "expected"),
[
("das", {"a": [True, False]}),
("fas", {"a": [False, True]}),
("asd", {"a": [False, False]}),
],
)
def test_ends_with(suffix: str, expected: dict[str, list[bool]]) -> None:
import dask.dataframe as dd

data = {"a": ["fdas", "edfas"]}
dfdd = dd.from_pandas(pd.DataFrame(data))
df = nw.from_native(dfdd)
result = df.with_columns(nw.col("a").str.ends_with(suffix))

compare_dicts(result, expected)


def test_contains() -> None:
import dask.dataframe as dd

data = {"pets": ["cat", "dog", "rabbit and parrot", "dove"]}
dfdd = dd.from_pandas(pd.DataFrame(data))
df = nw.from_native(dfdd)

result = df.with_columns(
case_insensitive_match=nw.col("pets").str.contains("(?i)parrot|Dove")
)
expected = {
"pets": ["cat", "dog", "rabbit and parrot", "dove"],
"case_insensitive_match": [False, False, True, True],
}
compare_dicts(result, expected)


@pytest.mark.parametrize(
("offset", "length", "expected"),
[(1, 2, {"a": ["da", "df"]}), (-2, None, {"a": ["as", "as"]})],
)
def test_str_slice(offset: int, length: int | None, expected: Any) -> None:
import dask.dataframe as dd

data = {"a": ["fdas", "edfas"]}
dfdd = dd.from_pandas(pd.DataFrame(data))
df = nw.from_native(dfdd)

result_frame = df.with_columns(nw.col("a").str.slice(offset, length))
compare_dicts(result_frame, expected)
Loading