Skip to content

Commit 6195872

Browse files
[backport 3.0.x] BUG: anchor whole alternation in Series.str.match for PyArrow string dtype (#66070) (#66076)
Co-authored-by: GAUTAM V DATLA <85986314+gautamvarmadatla@users.noreply.github.com>
1 parent 70313be commit 6195872

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎doc/source/whatsnew/v3.0.4.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Bug fixes
2929
- Bug in :meth:`DataFrame.iloc` silently ignoring the assignment when setting values with an unordered or duplicated column indexer on a :class:`DataFrame` whose values are referenced by another object (:issue:`65446`)
3030
- Bug in :meth:`DataFrame.to_sql` and :func:`read_sql_table` when using an ADBC engine where table and schema names were not quoted as SQL identifiers, causing failures for identifiers containing spaces or reserved words, and making it vulnerable to SQL injection (:issue:`65065`)
3131
- Bug in :meth:`Series.str.__getitem__` raising ``AttributeError`` when underlying array is :class:`ArrowExtensionArray` (:issue:`65112`)
32+
- Bug in :meth:`Series.str.match` and :meth:`Index.str.match` on PyArrow-backed string dtypes where a leading ``^`` only anchored the first branch of an alternation pattern (e.g. ``r"^foo|bar"``) (:issue:`66069`)
3233
- Bug in arithmetic adding or subtracting a non-tick :class:`DateOffset` (e.g. :class:`offsets.MonthEnd`, :class:`offsets.QuarterEnd`) to datetime data that could cause a segmentation fault when another thread was running concurrently, e.g. under ``pytest-xdist`` (:issue:`66031`)
3334

3435

‎pandas/core/arrays/_arrow_string_mixins.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,9 @@ def _str_match(
400400
flags: int = 0,
401401
na: Scalar | lib.NoDefault = lib.no_default,
402402
):
403-
if not pat.startswith("^"):
404-
pat = f"^({pat})"
403+
if pat.startswith("^"):
404+
pat = pat[1:]
405+
pat = f"^({pat})"
405406
return ArrowStringArrayMixin._str_contains(
406407
self, pat, case, flags, na, regex=True
407408
)

‎pandas/tests/strings/test_find_replace.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,21 @@ def test_match(any_string_dtype):
10911091
tm.assert_series_equal(result, expected)
10921092

10931093

1094+
@pytest.mark.parametrize(
1095+
"pat",
1096+
[r"^foo|bar", r"^bar|foo", r"^foo|bar$", r"^(foo)|bar"],
1097+
)
1098+
def test_match_anchored_alternation(any_string_dtype, pat):
1099+
# GH#66069
1100+
ser = Series(["xbar", "bar", "foo", "xfoo"], dtype=any_string_dtype)
1101+
result = ser.str.match(pat)
1102+
expected_dtype = (
1103+
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
1104+
)
1105+
expected = Series([False, True, True, False], dtype=expected_dtype)
1106+
tm.assert_series_equal(result, expected)
1107+
1108+
10941109
def test_match_mixed_object():
10951110
mixed = Series(
10961111
[

0 commit comments

Comments
 (0)