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

Support the case=False argument to str.contains #13290

Merged
merged 2 commits into from
May 10, 2023
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
35 changes: 21 additions & 14 deletions python/cudf/cudf/core/column/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def contains(
4 False
dtype: bool

The ``pat`` may also be a list of strings in which case
The ``pat`` may also be a sequence of strings in which case
the individual strings are searched in corresponding rows.

>>> s2 = cudf.Series(['house', 'dog', 'and', '', ''])
Expand All @@ -756,8 +756,6 @@ def contains(
4 <NA>
dtype: bool
""" # noqa W605
if case is not True:
raise NotImplementedError("`case` parameter is not yet supported")
if na is not np.nan:
raise NotImplementedError("`na` parameter is not yet supported")
if regex and isinstance(pat, re.Pattern):
Expand All @@ -767,22 +765,31 @@ def contains(
raise NotImplementedError(
"unsupported value for `flags` parameter"
)

if pat is None:
result_col = column.column_empty(
len(self._column), dtype="bool", masked=True
if regex and not case:
raise NotImplementedError(
"`case=False` only supported when `regex=False`"
)
elif is_scalar(pat):

if is_scalar(pat):
if regex:
result_col = libstrings.contains_re(self._column, pat, flags)
else:
result_col = libstrings.contains(
self._column, cudf.Scalar(pat, "str")
)
if case is False:
input_column = libstrings.to_lower(self._column)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Guessing libstrings doesn't have a casefold-like method right? https://docs.python.org/3/library/stdtypes.html#str.casefold

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@davidwendt is (or was this ever) on our radar?

Copy link
Contributor

Choose a reason for hiding this comment

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

No.

pat = cudf.Scalar(pat.lower(), dtype="str") # type: ignore
else:
input_column = self._column
pat = cudf.Scalar(pat, dtype="str") # type: ignore
result_col = libstrings.contains(input_column, pat)
else:
result_col = libstrings.contains_multiple(
self._column, column.as_column(pat, dtype="str")
)
# TODO: we silently ignore the `regex=` flag here
if case is False:
input_column = libstrings.to_lower(self._column)
pat = libstrings.to_lower(column.as_column(pat, dtype="str"))
else:
input_column = self._column
pat = column.as_column(pat, dtype="str")
result_col = libstrings.contains_multiple(input_column, pat)
return self._return_or_inplace(result_col)

def like(self, pat: str, esc: str = None) -> SeriesOrIndex:
Expand Down
11 changes: 11 additions & 0 deletions python/cudf/cudf/tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,17 @@ def test_string_contains(ps_gs, pat, regex, flags, flags_raise, na, na_raise):
assert_eq(expect, got)


def test_string_contains_case(ps_gs):
ps, gs = ps_gs
with pytest.raises(NotImplementedError):
gs.str.contains("A", case=False)
expected = ps.str.contains("A", regex=False, case=False)
got = gs.str.contains("A", regex=False, case=False)
assert_eq(expected, got)
got = gs.str.contains("a", regex=False, case=False)
assert_eq(expected, got)


@pytest.mark.parametrize(
"pat,esc,expect",
[
Expand Down