Skip to content

Commit 821f128

Browse files
raise error in case of regex with groups and as_indexer=False
1 parent 2ca3721 commit 821f128

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pandas/core/strings.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=None):
460460

461461
regex = re.compile(pat, flags=flags)
462462

463-
if as_indexer is not None:
463+
if (as_indexer is False) and (regex.groups > 0):
464+
raise ValueError("as_indexer=False with a pattern with groups is "
465+
"no longer supported. Use .str.extract(pat) instead")
466+
elif as_indexer is not None:
464467
# Previously, this keyword was used for changing the default but
465468
# deprecated behaviour. This keyword is now no longer needed.
466469
warnings.warn("'as_indexer' keyword was specified but will be ignored;"
@@ -1523,7 +1526,7 @@ def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
15231526
return self._wrap_result(result)
15241527

15251528
@copy(str_match)
1526-
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=False):
1529+
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
15271530
result = str_match(self._data, pat, case=case, flags=flags, na=na,
15281531
as_indexer=as_indexer)
15291532
return self._wrap_result(result)

pandas/tests/test_strings.py

+5
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,11 @@ def test_match(self):
525525
with tm.assert_produces_warning(UserWarning):
526526
result = values.str.match('.*BAD[_]+.*BAD', as_indexer=False)
527527
tm.assert_series_equal(result, exp)
528+
with tm.assert_produces_warning(UserWarning):
529+
result = values.str.match('.*(BAD[_]+).*(BAD)', as_indexer=True)
530+
tm.assert_series_equal(result, exp)
531+
self.assertRaises(ValueError, values.str.match, '.*(BAD[_]+).*(BAD)',
532+
as_indexer=False)
528533

529534
# mixed
530535
mixed = Series(['aBAD_BAD', NA, 'BAD_b_BAD', True, datetime.today(),

0 commit comments

Comments
 (0)