Skip to content

fix(typing): #983 return type of StringMethods.match #990

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 4 commits into from
Sep 4, 2024
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
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Index(IndexOpsMixin[S1]):
**kwargs,
) -> Self: ...
@property
def str(self) -> StringMethods[Self, MultiIndex]: ...
def str(self) -> StringMethods[Self, MultiIndex, np_ndarray_bool]: ...
def is_(self, other) -> bool: ...
def __len__(self) -> int: ...
def __array__(self, dtype=...) -> np.ndarray: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
) -> Series[S1]: ...
def to_period(self, freq: _str | None = ..., copy: _bool = ...) -> DataFrame: ...
@property
def str(self) -> StringMethods[Series, DataFrame]: ...
def str(self) -> StringMethods[Series, DataFrame, Series[bool]]: ...
@property
def dt(self) -> CombinedDatetimelikeProperties: ...
@property
Expand Down
7 changes: 5 additions & 2 deletions pandas-stubs/core/strings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ from pandas.core.base import NoNewAttributesMixin
from pandas._typing import (
JoinHow,
T,
np_ndarray_bool,
)

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

class StringMethods(NoNewAttributesMixin, Generic[T, _TS]):
class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM]):
def __init__(self, data: T) -> None: ...
def __getitem__(self, key: slice | int) -> T: ...
def __iter__(self) -> T: ...
Expand Down Expand Up @@ -100,7 +103,7 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS]):
) -> Series[bool]: ...
def match(
self, pat: str, case: bool = ..., flags: int = ..., na: Any = ...
) -> T: ...
) -> _TM: ...
def replace(
self,
pat: str,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def test_str_split() -> None:
check(assert_type(ind.str.split("-", expand=True), pd.MultiIndex), pd.MultiIndex)


def test_str_match() -> None:
i = pd.Index(
["applep", "bananap", "Cherryp", "DATEp", "eGGpLANTp", "123p", "23.45p"]
)
check(assert_type(i.str.match("pp"), npt.NDArray[np.bool_]), np.ndarray, np.bool_)


def test_index_rename() -> None:
ind = pd.Index([1, 2, 3], name="foo")
ind2 = ind.rename("goo")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ def test_string_accessors():
check(assert_type(s.str.ljust(80), pd.Series), pd.Series)
check(assert_type(s.str.lower(), pd.Series), pd.Series)
check(assert_type(s.str.lstrip("a"), pd.Series), pd.Series)
check(assert_type(s.str.match("pp"), pd.Series), pd.Series)
check(assert_type(s.str.match("pp"), "pd.Series[bool]"), pd.Series, np.bool_)
check(assert_type(s.str.normalize("NFD"), pd.Series), pd.Series)
check(assert_type(s.str.pad(80, "right"), pd.Series), pd.Series)
check(assert_type(s.str.partition("p"), pd.DataFrame), pd.DataFrame)
Expand Down
Loading