Skip to content

Commit 85e9049

Browse files
BUG: fix bug in str.match for Arrow backend with optional groups (#62410)
1 parent 7581d00 commit 85e9049

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

doc/source/whatsnew/v2.3.3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Bug fixes
3535
- Fix bug in :meth:`Series.str.replace` using named capture groups (e.g., ``\g<name>``) with the Arrow-backed dtype would raise an error (:issue:`57636`)
3636
- Fix regression in ``~Series.str.contains``, ``~Series.str.match`` and ``~Series.str.fullmatch``
3737
with a compiled regex and custom flags (:issue:`62240`)
38-
- Fix :meth:`Series.str.fullmatch` not matching patterns with groups correctly for the Arrow-backed string dtype (:issue:`61072`)
38+
- Fix :meth:`Series.str.match` and :meth:`Series.str.fullmatch` not matching patterns with groups correctly for the Arrow-backed string dtype (:issue:`61072`)
3939

4040

4141
Improvements and fixes for Copy-on-Write

pandas/core/arrays/_arrow_string_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def _str_match(
316316
na: Scalar | lib.NoDefault = lib.no_default,
317317
):
318318
if not pat.startswith("^"):
319-
pat = f"^{pat}"
319+
pat = f"^({pat})"
320320
return self._str_contains(pat, case, flags, na, regex=True)
321321

322322
def _str_fullmatch(

pandas/tests/strings/test_find_replace.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,30 @@ def test_match_compiled_regex(any_string_dtype):
973973
values.str.match(re.compile("ab"), flags=re.IGNORECASE)
974974

975975

976+
@pytest.mark.parametrize(
977+
"pat, case, exp",
978+
[
979+
["ab", False, [True, False]],
980+
["Ab", True, [False, False]],
981+
["bc", True, [False, False]],
982+
["a[a-z]{1}", False, [True, False]],
983+
["A[a-z]{1}", True, [False, False]],
984+
# https://github.com/pandas-dev/pandas/issues/61072
985+
["(bc)|(ab)", True, [True, False]],
986+
["((bc)|(ab))", True, [True, False]],
987+
],
988+
)
989+
def test_str_match_extra_cases(any_string_dtype, pat, case, exp):
990+
ser = Series(["abc", "Xab"], dtype=any_string_dtype)
991+
result = ser.str.match(pat, case=case)
992+
993+
expected_dtype = (
994+
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
995+
)
996+
expected = Series(exp, dtype=expected_dtype)
997+
tm.assert_series_equal(result, expected)
998+
999+
9761000
# --------------------------------------------------------------------------------------
9771001
# str.fullmatch
9781002
# --------------------------------------------------------------------------------------
@@ -1108,7 +1132,6 @@ def test_str_fullmatch_extra_cases(any_string_dtype, pat, case, na, exp):
11081132
expected_dtype = (
11091133
"object" if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
11101134
)
1111-
expected = Series([True, False, np.nan, False], dtype=expected_dtype)
11121135
expected = Series(exp, dtype=expected_dtype)
11131136
tm.assert_series_equal(result, expected)
11141137

0 commit comments

Comments
 (0)