Skip to content

Commit bf6da3b

Browse files
KhemkaranKhemkaran
authored andcommitted
moved new tests to strings/test_find_replace.py
1 parent 56e0280 commit bf6da3b

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

pandas/core/strings/object_array.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,15 @@ def rep(x, r):
248248

249249
def _str_match(
250250
self,
251-
pat: str,
251+
pat,
252252
case: bool = True,
253253
flags: int = 0,
254254
na: Scalar | lib.NoDefault = lib.no_default,
255255
):
256256
if not case:
257257
flags |= re.IGNORECASE
258-
258+
if isinstance(pat, re.Pattern):
259+
pat = pat.pattern
259260
regex = re.compile(pat, flags=flags)
260261

261262
f = lambda x: regex.match(x) is not None
@@ -270,7 +271,8 @@ def _str_fullmatch(
270271
):
271272
if not case:
272273
flags |= re.IGNORECASE
273-
274+
if isinstance(pat, re.Pattern):
275+
pat = pat.pattern
274276
regex = re.compile(pat, flags=flags)
275277

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

pandas/tests/extension/test_arrow.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,10 +1857,6 @@ def test_str_repeat():
18571857
["ab", False, True, [True, True]],
18581858
["a[a-z]{1}", False, None, [True, None]],
18591859
["A[a-z]{1}", True, None, [False, None]],
1860-
# GH#61952
1861-
[re.compile(r"ab"), False, None, [True, None]],
1862-
[re.compile(r"Abc"), True, None, [False, None]],
1863-
[re.compile(r"a[a-z]{1}"), False, None, [True, None]],
18641860
],
18651861
)
18661862
def test_str_match(pat, case, na, exp):
@@ -1884,10 +1880,6 @@ def test_str_match(pat, case, na, exp):
18841880
["abc\\$", False, None, [False, True, False, None]],
18851881
["Abc$", True, None, [False, False, False, None]],
18861882
["Abc\\$", True, None, [False, False, False, None]],
1887-
# GH#61952
1888-
[re.compile(r"abc"), False, None, [True, True, False, None]],
1889-
[re.compile(r"abc$"), False, None, [True, False, False, None]],
1890-
[re.compile(r"a[a-z]{2}"), False, None, [True, True, False, None]],
18911883
],
18921884
)
18931885
def test_str_fullmatch(pat, case, na, exp):

pandas/tests/strings/test_find_replace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,16 @@ def test_match_case_kwarg(any_string_dtype):
818818
tm.assert_series_equal(result, expected)
819819

820820

821+
def test_match_compiled_regex(any_string_dtype):
822+
values = Series(["ab", "AB", "abc", "ABC"], dtype=any_string_dtype)
823+
result = values.str.match(re.compile(r"ab"), case=False)
824+
expected_dtype = (
825+
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
826+
)
827+
expected = Series([True, True, True, True], dtype=expected_dtype)
828+
tm.assert_series_equal(result, expected)
829+
830+
821831
# --------------------------------------------------------------------------------------
822832
# str.fullmatch
823833
# --------------------------------------------------------------------------------------
@@ -887,6 +897,16 @@ def test_fullmatch_case_kwarg(any_string_dtype):
887897
tm.assert_series_equal(result, expected)
888898

889899

900+
def test_fullmatch_compiled_regex(any_string_dtype):
901+
values = Series(["ab", "AB", "abc", "ABC"], dtype=any_string_dtype)
902+
result = values.str.fullmatch(re.compile(r"ab"), case=False)
903+
expected_dtype = (
904+
np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean"
905+
)
906+
expected = Series([True, True, False, False], dtype=expected_dtype)
907+
tm.assert_series_equal(result, expected)
908+
909+
890910
# --------------------------------------------------------------------------------------
891911
# str.findall
892912
# --------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)