Skip to content

Commit 6024069

Browse files
committed
BUG: Fix Series.str.contains with compiled regex on Arrow strings (closes #61942)
1 parent db54fad commit 6024069

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

pandas/core/strings/accessor.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,36 @@ def contains(
13501350
4 False
13511351
dtype: bool
13521352
"""
1353-
if regex and re.compile(pat).groups:
1354-
warnings.warn(
1355-
"This pattern is interpreted as a regular expression, and has "
1356-
"match groups. To actually get the groups, use str.extract.",
1357-
UserWarning,
1358-
stacklevel=find_stack_level(),
1359-
)
1353+
from pandas.core.dtypes.dtypes import ArrowDtype
1354+
import re
1355+
1356+
#---Handle Arrow-based string arrays with compiled regex patterns---
1357+
# Arrow backend does not support compiled regex objects or Python regex flags.
1358+
# If a compiled regex is passsed, only allow it if no flags are set.
1359+
if isinstance(self._data.dtype, ArrowDtype) and isinstance(pat, re.Pattern):
1360+
if flags != 0:
1361+
raise NotImplementedError(
1362+
"Series.str.contains() with a compiled regex pattern and flag is not supported"
1363+
"for Arrow-backed string arrays."
1364+
)
1365+
pat = pat.pattern
1366+
1367+
regex = True
13601368

1369+
if regex:
1370+
try:
1371+
_compiled =pat if isinstance(pat, re.Pattern) else re.compile(pat, flags= flags)
1372+
if _compiled.groups:
1373+
warnings.warn(
1374+
"This pattern is interpreted as a regular expression, and has "
1375+
"match groups. To actually get the groups, use str.extract.",
1376+
UserWarning,
1377+
stacklevel=find_stack_level(),
1378+
)
1379+
except re.error as e:
1380+
raise ValueError(f" Invalid regex pattern passed to str.contains(): {e}"
1381+
) from e
1382+
13611383
result = self._data.array._str_contains(pat, case, flags, na, regex)
13621384
return self._wrap_result(result, fill_value=na, returns_string=False)
13631385

0 commit comments

Comments
 (0)