Skip to content

[backport 2.3.x] BUG: Fix Series.str.contains with compiled regex on Arrow string dtype (#61946) #62116

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
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v2.3.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Bug fixes
- Fix :meth:`~DataFrame.to_json` with ``orient="table"`` to correctly use the
"string" type in the JSON Table Schema for :class:`StringDtype` columns
(:issue:`61889`)
- Fixed ``~Series.str.match`` and ``~Series.str.fullmatch`` with compiled regex
for the Arrow-backed string dtype (:issue:`61964`)
- Fixed ``~Series.str.match``, ``~Series.str.fullmatch`` and ``~Series.str.contains``
with compiled regex for the Arrow-backed string dtype (:issue:`61964`, :issue:`61942`)

.. ---------------------------------------------------------------------------
.. _whatsnew_232.contributors:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ def _str_contains(
):
if flags:
return super()._str_contains(pat, case, flags, na, regex)
if isinstance(pat, re.Pattern):
pat = pat.pattern

return ArrowStringArrayMixin._str_contains(self, pat, case, flags, na, regex)

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/strings/test_find_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,19 @@ def test_contains_nan(any_string_dtype):
tm.assert_series_equal(result, expected)


def test_contains_compiled_regex(any_string_dtype):
# GH#61942
ser = Series(["foo", "bar", "baz"], dtype=any_string_dtype)
pat = re.compile("ba.")
result = ser.str.contains(pat)

expected_dtype = (
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
)
expected = Series([False, True, True], dtype=expected_dtype)
tm.assert_series_equal(result, expected)


# --------------------------------------------------------------------------------------
# str.startswith
# --------------------------------------------------------------------------------------
Expand Down
Loading