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 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
14 changes: 10 additions & 4 deletions pandas/core/arrays/_arrow_string_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,29 @@ def _str_contains(

def _str_match(
self,
pat: str,
pat: str | re.Pattern,
case: bool = True,
flags: int = 0,
na: Scalar | lib.NoDefault = lib.no_default,
):
if not pat.startswith("^"):
if isinstance(pat, re.Pattern):
# GH#61952
pat = pat.pattern
if isinstance(pat, str) and not pat.startswith("^"):
pat = f"^{pat}"
return self._str_contains(pat, case, flags, na, regex=True)

def _str_fullmatch(
self,
pat,
pat: str | re.Pattern,
case: bool = True,
flags: int = 0,
na: Scalar | lib.NoDefault = lib.no_default,
):
if not pat.endswith("$") or pat.endswith("\\$"):
if isinstance(pat, re.Pattern):
# GH#61952
pat = pat.pattern
if isinstance(pat, str) and (not pat.endswith("$") or pat.endswith("\\$")):
pat = f"{pat}$"
return self._str_match(pat, case, flags, na)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,8 @@ def match(self, pat: str, case: bool = True, flags: int = 0, na=lib.no_default):

Parameters
----------
pat : str
Character sequence.
pat : str or compiled regex
Character sequence or regular expression.
case : bool, default True
If True, case sensitive.
flags : int, default 0 (no flags)
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,15 @@ def rep(x, r):

def _str_match(
self,
pat: str,
pat: str | re.Pattern,
case: bool = True,
flags: int = 0,
na: Scalar | lib.NoDefault = lib.no_default,
):
if not case:
flags |= re.IGNORECASE

if isinstance(pat, re.Pattern):
pat = pat.pattern
regex = re.compile(pat, flags=flags)

f = lambda x: regex.match(x) is not None
Expand All @@ -270,7 +271,8 @@ def _str_fullmatch(
):
if not case:
flags |= re.IGNORECASE

if isinstance(pat, re.Pattern):
pat = pat.pattern
regex = re.compile(pat, flags=flags)

f = lambda x: regex.fullmatch(x) is not None
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/strings/test_find_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,17 @@ def test_match_case_kwarg(any_string_dtype):
tm.assert_series_equal(result, expected)


def test_match_compiled_regex(any_string_dtype):
# GH#61952
values = Series(["ab", "AB", "abc", "ABC"], dtype=any_string_dtype)
result = values.str.match(re.compile(r"ab"), case=False)
expected_dtype = (
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
)
expected = Series([True, True, True, True], dtype=expected_dtype)
tm.assert_series_equal(result, expected)


# --------------------------------------------------------------------------------------
# str.fullmatch
# --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -887,6 +898,17 @@ def test_fullmatch_case_kwarg(any_string_dtype):
tm.assert_series_equal(result, expected)


def test_fullmatch_compiled_regex(any_string_dtype):
# GH#61952
values = Series(["ab", "AB", "abc", "ABC"], dtype=any_string_dtype)
result = values.str.fullmatch(re.compile(r"ab"), case=False)
expected_dtype = (
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
)
expected = Series([True, True, False, False], dtype=expected_dtype)
tm.assert_series_equal(result, expected)


# --------------------------------------------------------------------------------------
# str.findall
# --------------------------------------------------------------------------------------
Expand Down
Loading