Skip to content

Commit 920aa10

Browse files
KhemkaranKhemkaran
authored andcommitted
str.fullmatch() and str.match() with complied regex fix issue 61952
1 parent a067fff commit 920aa10

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pandas/core/arrays/_arrow_string_mixins.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,14 @@ def _str_contains(
304304

305305
def _str_match(
306306
self,
307-
pat: str,
307+
pat,
308308
case: bool = True,
309309
flags: int = 0,
310310
na: Scalar | lib.NoDefault = lib.no_default,
311311
):
312+
if isinstance(pat, re.Pattern):
313+
# GH#61952
314+
pat = pat.pattern
312315
if not pat.startswith("^"):
313316
pat = f"^{pat}"
314317
return self._str_contains(pat, case, flags, na, regex=True)
@@ -320,6 +323,9 @@ def _str_fullmatch(
320323
flags: int = 0,
321324
na: Scalar | lib.NoDefault = lib.no_default,
322325
):
326+
if isinstance(pat, re.Pattern):
327+
# GH#61952
328+
pat = pat.pattern
323329
if not pat.endswith("$") or pat.endswith("\\$"):
324330
pat = f"{pat}$"
325331
return self._str_match(pat, case, flags, na)

pandas/tests/extension/test_arrow.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,10 @@ def test_str_repeat():
18571857
["ab", False, True, [True, True]],
18581858
["a[a-z]{1}", False, None, [True, None]],
18591859
["A[a-z]{1}", True, None, [False, None]],
1860+
# GH#61952
1861+
[re.compile(r"ab"), False, None, [True, None]],
1862+
[re.compile(r"Abc"), True, None, [False, None]],
1863+
[re.compile(r"a[a-z]{1}"), False, None, [True, None]],
18601864
],
18611865
)
18621866
def test_str_match(pat, case, na, exp):
@@ -1880,6 +1884,10 @@ def test_str_match(pat, case, na, exp):
18801884
["abc\\$", False, None, [False, True, False, None]],
18811885
["Abc$", True, None, [False, False, False, None]],
18821886
["Abc\\$", True, None, [False, False, False, None]],
1887+
# GH#61952
1888+
[re.compile(r"abc"), False, None, [True, True, False, None]],
1889+
[re.compile(r"abc$"), False, None, [True, False, False, None]],
1890+
[re.compile(r"a[a-z]{2}"), False, None, [True, True, False, None]],
18831891
],
18841892
)
18851893
def test_str_fullmatch(pat, case, na, exp):

0 commit comments

Comments
 (0)