Skip to content

BUG FIX: Using Series.str.fullmatch() and Series.str.match() with a compiled regex fails with arrow strings #61964

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 7 additions & 1 deletion pandas/core/arrays/_arrow_string_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,14 @@ def _str_contains(

def _str_match(
self,
pat: str,
pat,
case: bool = True,
flags: int = 0,
na: Scalar | lib.NoDefault = lib.no_default,
):
if isinstance(pat, re.Pattern):
# GH#61952
pat = pat.pattern
if not pat.startswith("^"):
pat = f"^{pat}"
return self._str_contains(pat, case, flags, na, regex=True)
Expand All @@ -320,6 +323,9 @@ def _str_fullmatch(
flags: int = 0,
na: Scalar | lib.NoDefault = lib.no_default,
):
if isinstance(pat, re.Pattern):
# GH#61952
pat = pat.pattern
if not pat.endswith("$") or pat.endswith("\\$"):
pat = f"{pat}$"
return self._str_match(pat, case, flags, na)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,10 @@ def test_str_repeat():
["ab", False, True, [True, True]],
["a[a-z]{1}", False, None, [True, None]],
["A[a-z]{1}", True, None, [False, None]],
# GH#61952
[re.compile(r"ab"), False, None, [True, None]],
[re.compile(r"Abc"), True, None, [False, None]],
[re.compile(r"a[a-z]{1}"), False, None, [True, None]],
],
)
def test_str_match(pat, case, na, exp):
Expand All @@ -1880,6 +1884,10 @@ def test_str_match(pat, case, na, exp):
["abc\\$", False, None, [False, True, False, None]],
["Abc$", True, None, [False, False, False, None]],
["Abc\\$", True, None, [False, False, False, None]],
# GH#61952
[re.compile(r"abc"), False, None, [True, True, False, None]],
[re.compile(r"abc$"), False, None, [True, False, False, None]],
[re.compile(r"a[a-z]{2}"), False, None, [True, True, False, None]],
],
)
def test_str_fullmatch(pat, case, na, exp):
Expand Down
Loading