Skip to content

Commit 532fc66

Browse files
authored
fix(typing): #983 return type of StringMethods.match (#990)
* fix(typing): #983 return type of StringMethods.match * feat(string): #983 tests for the fix * feat: #983 new TypeVar, following #990 (comment) * fix(comment): #983 use `np_ndarray_bool` from `pandas._typing` in stubs @Dr-Irv #990 (review)
1 parent a915ae7 commit 532fc66

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class Index(IndexOpsMixin[S1]):
260260
**kwargs,
261261
) -> Self: ...
262262
@property
263-
def str(self) -> StringMethods[Self, MultiIndex]: ...
263+
def str(self) -> StringMethods[Self, MultiIndex, np_ndarray_bool]: ...
264264
def is_(self, other) -> bool: ...
265265
def __len__(self) -> int: ...
266266
def __array__(self, dtype=...) -> np.ndarray: ...

pandas-stubs/core/series.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
11631163
) -> Series[S1]: ...
11641164
def to_period(self, freq: _str | None = ..., copy: _bool = ...) -> DataFrame: ...
11651165
@property
1166-
def str(self) -> StringMethods[Series, DataFrame]: ...
1166+
def str(self) -> StringMethods[Series, DataFrame, Series[bool]]: ...
11671167
@property
11681168
def dt(self) -> CombinedDatetimelikeProperties: ...
11691169
@property

pandas-stubs/core/strings.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ from pandas.core.base import NoNewAttributesMixin
2323
from pandas._typing import (
2424
JoinHow,
2525
T,
26+
np_ndarray_bool,
2627
)
2728

2829
# The _TS type is what is used for the result of str.split with expand=True
2930
_TS = TypeVar("_TS", DataFrame, MultiIndex)
31+
# The _TM type is what is used for the result of str.match
32+
_TM = TypeVar("_TM", Series[bool], np_ndarray_bool)
3033

31-
class StringMethods(NoNewAttributesMixin, Generic[T, _TS]):
34+
class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM]):
3235
def __init__(self, data: T) -> None: ...
3336
def __getitem__(self, key: slice | int) -> T: ...
3437
def __iter__(self) -> T: ...
@@ -105,7 +108,7 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS]):
105108
) -> Series[bool]: ...
106109
def match(
107110
self, pat: str, case: bool = ..., flags: int = ..., na: Any = ...
108-
) -> T: ...
111+
) -> _TM: ...
109112
def replace(
110113
self,
111114
pat: str,

tests/test_indexes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ def test_str_split() -> None:
113113
check(assert_type(ind.str.split("-", expand=True), pd.MultiIndex), pd.MultiIndex)
114114

115115

116+
def test_str_match() -> None:
117+
i = pd.Index(
118+
["applep", "bananap", "Cherryp", "DATEp", "eGGpLANTp", "123p", "23.45p"]
119+
)
120+
check(assert_type(i.str.match("pp"), npt.NDArray[np.bool_]), np.ndarray, np.bool_)
121+
122+
116123
def test_index_rename() -> None:
117124
ind = pd.Index([1, 2, 3], name="foo")
118125
ind2 = ind.rename("goo")

tests/test_series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ def test_string_accessors():
14861486
check(assert_type(s.str.ljust(80), pd.Series), pd.Series)
14871487
check(assert_type(s.str.lower(), pd.Series), pd.Series)
14881488
check(assert_type(s.str.lstrip("a"), pd.Series), pd.Series)
1489-
check(assert_type(s.str.match("pp"), pd.Series), pd.Series)
1489+
check(assert_type(s.str.match("pp"), "pd.Series[bool]"), pd.Series, np.bool_)
14901490
check(assert_type(s.str.normalize("NFD"), pd.Series), pd.Series)
14911491
check(assert_type(s.str.pad(80, "right"), pd.Series), pd.Series)
14921492
check(assert_type(s.str.partition("p"), pd.DataFrame), pd.DataFrame)

0 commit comments

Comments
 (0)