Skip to content
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