-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ strings | |
regex_flags | ||
regex_program | ||
repeat | ||
replace_re | ||
replace | ||
side_type | ||
slice | ||
|
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/strings/replace_re.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
========== | ||
replace_re | ||
========== | ||
|
||
.. automodule:: pylibcudf.strings.replace_re | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
regex_program, | ||
repeat, | ||
replace, | ||
replace_re, | ||
side_type, | ||
slice, | ||
split, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
python/pylibcudf/pylibcudf/tests/test_string_replace_re.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pyarrow.compute as pc | ||
import pylibcudf as plc | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
|
||
@pytest.mark.parametrize("max_replace_count", [-1, 1]) | ||
def test_replace_re_regex_program_scalar(max_replace_count): | ||
arr = pa.array(["foo", "fuz", None]) | ||
pat = "f." | ||
repl = "ba" | ||
result = plc.strings.replace_re.replace_re( | ||
plc.interop.from_arrow(arr), | ||
plc.strings.regex_program.RegexProgram.create( | ||
pat, plc.strings.regex_flags.RegexFlags.DEFAULT | ||
), | ||
plc.interop.from_arrow(pa.scalar(repl)), | ||
max_replace_count=max_replace_count, | ||
) | ||
expected = pc.replace_substring_regex( | ||
arr, | ||
pat, | ||
repl, | ||
max_replacements=max_replace_count | ||
if max_replace_count != -1 | ||
else None, | ||
) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"flags", | ||
[ | ||
plc.strings.regex_flags.RegexFlags.DEFAULT, | ||
plc.strings.regex_flags.RegexFlags.DOTALL, | ||
], | ||
) | ||
def test_replace_re_list_str_columns(flags): | ||
arr = pa.array(["foo", "fuz", None]) | ||
pats = ["oo", "uz"] | ||
repls = ["a", "b"] | ||
result = plc.strings.replace_re.replace_re( | ||
plc.interop.from_arrow(arr), | ||
pats, | ||
plc.interop.from_arrow(pa.array(repls)), | ||
flags=flags, | ||
) | ||
expected = arr | ||
for pat, repl in zip(pats, repls): | ||
expected = pc.replace_substring_regex( | ||
expected, | ||
pat, | ||
repl, | ||
) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_replace_with_backrefs(): | ||
arr = pa.array(["Z756", None]) | ||
result = plc.strings.replace_re.replace_with_backrefs( | ||
plc.interop.from_arrow(arr), | ||
plc.strings.regex_program.RegexProgram.create( | ||
"(\\d)(\\d)", plc.strings.regex_flags.RegexFlags.DEFAULT | ||
), | ||
"V\\2\\1", | ||
) | ||
expected = pa.array(["ZV576", None]) | ||
assert_column_eq(result, expected) |