Skip to content

[ENH] Add "fullmatch" matching mode to Series.str [#32806] #32807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Other enhancements
- `OptionError` is now exposed in `pandas.errors` (:issue:`27553`)
- :func:`timedelta_range` will now infer a frequency when passed ``start``, ``stop``, and ``periods`` (:issue:`32377`)
- Positional slicing on a :class:`IntervalIndex` now supports slices with ``step > 1`` (:issue:`31658`)
- :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`).
- :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`)
-

Expand Down
45 changes: 44 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ def rep(x, r):

def str_match(arr, pat, case=True, flags=0, na=np.nan):
"""
Determine if each string matches a regular expression.
Determine if each string starts with a match of a regular expression.

Parameters
----------
Expand All @@ -808,6 +808,7 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan):

See Also
--------
fullmatch : Stricter matching that requires the entire string to match.
contains : Analogous, but less strict, relying on re.search instead of
re.match.
extract : Extract matched groups.
Expand All @@ -823,6 +824,42 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan):
return _na_map(f, arr, na, dtype=dtype)


def str_fullmatch(arr, pat, case=True, flags=0, na=np.nan):
"""
Determine if each string entirely matches a regular expression.

Parameters
----------
pat : str
Character sequence or regular expression.
case : bool, default True
If True, case sensitive.
flags : int, default 0 (no flags)
Regex module flags, e.g. re.IGNORECASE.
na : default NaN
Fill value for missing values.

Returns
-------
Series/array of boolean values

See Also
--------
match : Similar, but also returns `True` when only a *prefix* of the string
matches the regular expression.
extract : Extract matched groups.
"""
if not case:
flags |= re.IGNORECASE

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

dtype = bool
f = lambda x: regex.fullmatch(x) is not None

return _na_map(f, arr, na, dtype=dtype)


def _get_single_group_name(rx):
try:
return list(rx.groupindex.keys()).pop()
Expand Down Expand Up @@ -2762,6 +2799,12 @@ def match(self, pat, case=True, flags=0, na=np.nan):
result = str_match(self._parent, pat, case=case, flags=flags, na=na)
return self._wrap_result(result, fill_value=na, returns_string=False)

@copy(str_fullmatch)
@forbid_nonstring_types(["bytes"])
def fullmatch(self, pat, case=True, flags=0, na=np.nan):
result = str_fullmatch(self._parent, pat, case=case, flags=flags, na=na)
return self._wrap_result(result, fill_value=na, returns_string=False)

@copy(str_replace)
@forbid_nonstring_types(["bytes"])
def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
Expand Down
26 changes: 24 additions & 2 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def assert_series_or_index_equal(left, right):
("join", (",",), {}),
("ljust", (10,), {}),
("match", ("a",), {}),
("fullmatch", ("a",), {}),
("normalize", ("NFC",), {}),
("pad", (10,), {}),
("partition", (" ",), {"expand": False}),
Expand Down Expand Up @@ -1176,9 +1177,9 @@ def test_match(self):
exp = Series([True, np.nan, False])
tm.assert_series_equal(result, exp)

values = Series(["fooBAD__barBAD", np.nan, "foo"])
values = Series(["fooBAD__barBAD", "BAD_BADleroybrown", np.nan, "foo"])
result = values.str.match(".*BAD[_]+.*BAD")
exp = Series([True, np.nan, False])
exp = Series([True, True, np.nan, False])
tm.assert_series_equal(result, exp)

# mixed
Expand Down Expand Up @@ -1208,6 +1209,27 @@ def test_match(self):
exp = Series([True, np.nan, np.nan])
tm.assert_series_equal(exp, res)

def test_fullmatch(self):
values = Series(["fooBAD__barBAD", "BAD_BADleroybrown", np.nan, "foo"])
result = values.str.fullmatch(".*BAD[_]+.*BAD")
exp = Series([True, False, np.nan, False])
tm.assert_series_equal(result, exp)

# Make sure that flags work
from re import IGNORECASE

result = values.str.fullmatch(".*Bad[_]+.*bad", flags=IGNORECASE)
tm.assert_series_equal(result, exp)

# Make sure that the new string arrays work
string_values = Series(
["fooBAD__barBAD", "BAD_BADleroybrown", np.nan, "foo"], dtype="string"
)
result = string_values.str.fullmatch(".*BAD[_]+.*BAD")
# Result is nullable boolean with StringDtype
string_exp = Series([True, False, np.nan, False], dtype="boolean")
tm.assert_series_equal(result, string_exp)

def test_extract_expand_None(self):
values = Series(["fooBAD__barBAD", np.nan, "foo"])
with pytest.raises(ValueError, match="expand must be True or False"):
Expand Down