Skip to content

Commit a2bae51

Browse files
raise error in case of regex with groups and as_indexer=False
1 parent 87446c3 commit a2bae51

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pandas/core/strings.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=None):
477477
flags : int, default 0 (no flags)
478478
re module flags, e.g. re.IGNORECASE
479479
na : default NaN, fill value for missing values.
480-
as_indexer : ignored
481480
482481
Returns
483482
-------
@@ -495,7 +494,10 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=None):
495494

496495
regex = re.compile(pat, flags=flags)
497496

498-
if as_indexer is not None:
497+
if (as_indexer is False) and (regex.groups > 0):
498+
raise ValueError("as_indexer=False with a pattern with groups is no "
499+
"longer supported. Use '.str.extract(pat)' instead")
500+
elif as_indexer is not None:
499501
# Previously, this keyword was used for changing the default but
500502
# deprecated behaviour. This keyword is now no longer needed.
501503
warnings.warn("'as_indexer' keyword was specified but will be ignored;"
@@ -1558,7 +1560,7 @@ def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
15581560
return self._wrap_result(result)
15591561

15601562
@copy(str_match)
1561-
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=False):
1563+
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=None):
15621564
result = str_match(self._data, pat, case=case, flags=flags, na=na,
15631565
as_indexer=as_indexer)
15641566
return self._wrap_result(result)

pandas/tests/test_strings.py

+5
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@ def test_match(self):
580580
with tm.assert_produces_warning(UserWarning):
581581
result = values.str.match('.*BAD[_]+.*BAD', as_indexer=False)
582582
tm.assert_series_equal(result, exp)
583+
with tm.assert_produces_warning(UserWarning):
584+
result = values.str.match('.*(BAD[_]+).*(BAD)', as_indexer=True)
585+
tm.assert_series_equal(result, exp)
586+
self.assertRaises(ValueError, values.str.match, '.*(BAD[_]+).*(BAD)',
587+
as_indexer=False)
583588

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

0 commit comments

Comments
 (0)