Skip to content

Commit a915ae7

Browse files
authored
Allow str.contains to accept a regex as well as a string (#989)
* Allow str.contains to accept a regex as well as a string * Apply black formatting
1 parent b246fcf commit a915ae7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pandas-stubs/core/strings.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS]):
9696
def get(self, i: int) -> T: ...
9797
def join(self, sep: str) -> T: ...
9898
def contains(
99-
self, pat: str, case: bool = ..., flags: int = ..., na=..., regex: bool = ...
99+
self,
100+
pat: str | re.Pattern,
101+
case: bool = ...,
102+
flags: int = ...,
103+
na=...,
104+
regex: bool = ...,
100105
) -> Series[bool]: ...
101106
def match(
102107
self, pat: str, case: bool = ..., flags: int = ..., na: Any = ...

tests/test_series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,11 @@ def test_string_accessors():
14521452
check(assert_type(s.str.cat(sep="X"), str), str)
14531453
check(assert_type(s.str.center(10), pd.Series), pd.Series)
14541454
check(assert_type(s.str.contains("a"), "pd.Series[bool]"), pd.Series, np.bool_)
1455+
check(
1456+
assert_type(s.str.contains(re.compile(r"a")), "pd.Series[bool]"),
1457+
pd.Series,
1458+
np.bool_,
1459+
)
14551460
check(assert_type(s.str.count("pp"), "pd.Series[int]"), pd.Series, np.integer)
14561461
check(assert_type(s.str.decode("utf-8"), pd.Series), pd.Series)
14571462
check(assert_type(s.str.encode("latin-1"), pd.Series), pd.Series)

0 commit comments

Comments
 (0)