@@ -1350,14 +1350,36 @@ def contains(
1350
1350
4 False
1351
1351
dtype: bool
1352
1352
"""
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
1360
1368
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
+
1361
1383
result = self ._data .array ._str_contains (pat , case , flags , na , regex )
1362
1384
return self ._wrap_result (result , fill_value = na , returns_string = False )
1363
1385
0 commit comments