Skip to content

[ArrowStringArray] PERF: use pa.compute.match_substring_regex for str.match if available #41326

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

Merged
merged 1 commit into from
May 5, 2021
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
8 changes: 8 additions & 0 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Dtype,
NpDtype,
PositionalIndexer,
Scalar,
type_t,
)
from pandas.util._decorators import doc
Expand Down Expand Up @@ -808,6 +809,13 @@ def _str_endswith(self, pat, na=None):
else:
return super()._str_endswith(pat, na)

def _str_match(
self, pat: str, case: bool = True, flags: int = 0, na: Scalar = None
):
if not pat.startswith("^"):
pat = "^" + pat
return self._str_contains(pat, case, flags, na, regex=True)

def _str_isalnum(self):
result = pc.utf8_is_alnum(self._data)
return BooleanDtype().__from_arrow__(result)
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/strings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ def _str_repeat(self, repeats):

@abc.abstractmethod
def _str_match(
self,
pat: Union[str, Pattern],
case: bool = True,
flags: int = 0,
na: Scalar = np.nan,
self, pat: str, case: bool = True, flags: int = 0, na: Scalar = np.nan
):
pass

Expand Down
6 changes: 1 addition & 5 deletions pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ def rep(x, r):
return result

def _str_match(
self,
pat: Union[str, Pattern],
case: bool = True,
flags: int = 0,
na: Scalar = None,
self, pat: str, case: bool = True, flags: int = 0, na: Scalar = None
):
if not case:
flags |= re.IGNORECASE
Expand Down
74 changes: 53 additions & 21 deletions pandas/tests/strings/test_find_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,39 @@ def test_replace_literal(any_string_dtype):
values.str.replace(compiled_pat, "", regex=False)


def test_match():
def test_match(any_string_dtype):
# New match behavior introduced in 0.13
values = Series(["fooBAD__barBAD", np.nan, "foo"])
expected_dtype = "object" if any_string_dtype == "object" else "boolean"

values = Series(["fooBAD__barBAD", np.nan, "foo"], dtype=any_string_dtype)
result = values.str.match(".*(BAD[_]+).*(BAD)")
exp = Series([True, np.nan, False])
tm.assert_series_equal(result, exp)
expected = Series([True, np.nan, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)

values = Series(["fooBAD__barBAD", "BAD_BADleroybrown", np.nan, "foo"])
values = Series(
["fooBAD__barBAD", "BAD_BADleroybrown", np.nan, "foo"], dtype=any_string_dtype
)
result = values.str.match(".*BAD[_]+.*BAD")
exp = Series([True, True, np.nan, False])
tm.assert_series_equal(result, exp)
expected = Series([True, True, np.nan, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)

# mixed
result = values.str.match("BAD[_]+.*BAD")
expected = Series([False, True, np.nan, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)

values = Series(
["fooBAD__barBAD", "^BAD_BADleroybrown", np.nan, "foo"], dtype=any_string_dtype
)
result = values.str.match("^BAD[_]+.*BAD")
expected = Series([False, False, np.nan, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)

result = values.str.match("\\^BAD[_]+.*BAD")
expected = Series([False, True, np.nan, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)


def test_match_mixed_object():
mixed = Series(
[
"aBAD_BAD",
Expand All @@ -435,22 +455,34 @@ def test_match():
2.0,
]
)
rs = Series(mixed).str.match(".*(BAD[_]+).*(BAD)")
xp = Series([True, np.nan, True, np.nan, np.nan, False, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_series_equal(rs, xp)
result = Series(mixed).str.match(".*(BAD[_]+).*(BAD)")
expected = Series(
[True, np.nan, True, np.nan, np.nan, False, np.nan, np.nan, np.nan]
)
assert isinstance(result, Series)
tm.assert_series_equal(result, expected)


# na GH #6609
res = Series(["a", 0, np.nan]).str.match("a", na=False)
exp = Series([True, False, False])
tm.assert_series_equal(exp, res)
res = Series(["a", 0, np.nan]).str.match("a")
exp = Series([True, np.nan, np.nan])
tm.assert_series_equal(exp, res)
def test_match_na_kwarg(any_string_dtype):
# GH #6609
s = Series(["a", "b", np.nan], dtype=any_string_dtype)

values = Series(["ab", "AB", "abc", "ABC"])
result = s.str.match("a", na=False)
expected_dtype = np.bool_ if any_string_dtype == "object" else "boolean"
expected = Series([True, False, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)

result = s.str.match("a")
expected_dtype = "object" if any_string_dtype == "object" else "boolean"
expected = Series([True, False, np.nan], dtype=expected_dtype)
tm.assert_series_equal(result, expected)


def test_match_case_kwarg(any_string_dtype):
values = Series(["ab", "AB", "abc", "ABC"], dtype=any_string_dtype)
result = values.str.match("ab", case=False)
expected = Series([True, True, True, True])
expected_dtype = np.bool_ if any_string_dtype == "object" else "boolean"
expected = Series([True, True, True, True], dtype=expected_dtype)
tm.assert_series_equal(result, expected)


Expand Down