Skip to content
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

GH1074 Add type hint Series[list[str]] for Series.str.split with expand=False #1075

Merged
merged 8 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ S1 = TypeVar(
| Period
| Interval
| CategoricalDtype
| BaseOffset,
| BaseOffset
| list[str],
)

S2 = TypeVar(
Expand All @@ -566,7 +567,8 @@ S2 = TypeVar(
| Period
| Interval
| CategoricalDtype
| BaseOffset,
| BaseOffset
| list[str],
)

IndexingInt: TypeAlias = (
Expand Down
6 changes: 3 additions & 3 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ class Series(IndexOpsMixin[S1], NDFrame):
copy: bool = ...,
) -> Series[float]: ...
@overload
def __new__( # type: ignore[overload-overlap]
def __new__(
cls,
data: Sequence[Never],
data: Sequence[str],
index: Axes | None = ...,
*,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
) -> Series[Any]: ...
) -> Series[str]: ...
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
@overload
def __new__(
cls,
Expand Down
4 changes: 4 additions & 0 deletions pandas-stubs/core/strings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM]):
self, pat: str = ..., *, n: int = ..., expand: Literal[True], regex: bool = ...
) -> _TS: ...
@overload
def split(
self, pat: str = ..., *, n: int = ..., expand: Literal[False], regex: bool = ...
) -> Series[list[str]]: ...
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
@overload
def split(
self, pat: str = ..., *, n: int = ..., expand: bool = ..., regex: bool = ...
) -> T: ...
Expand Down
6 changes: 3 additions & 3 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3767,9 +3767,9 @@ class MyDict(TypedDict):


def test_series_empty_dtype() -> None:
"""Test for the creation of a Series from an empty list GH571 to map to a Series[Any]."""
"""Test for the creation of a Series from an empty list GH571 to map to a Series[str]."""
new_tab: Sequence[Never] = [] # need to be typehinted to please mypy
check(assert_type(pd.Series(new_tab), "pd.Series[Any]"), pd.Series)
check(assert_type(pd.Series([]), "pd.Series[Any]"), pd.Series)
check(assert_type(pd.Series(new_tab), "pd.Series[str]"), pd.Series)
check(assert_type(pd.Series([]), "pd.Series[str]"), pd.Series)
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved
# ensure that an empty string does not get matched to Sequence[Never]
check(assert_type(pd.Series(""), "pd.Series[str]"), pd.Series)
3 changes: 3 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,9 @@ def test_string_accessors():
check(assert_type(s.str.split("a"), pd.Series), pd.Series)
pan-vlados marked this conversation as resolved.
Show resolved Hide resolved
# GH 194
check(assert_type(s.str.split("a", expand=True), pd.DataFrame), pd.DataFrame)
check(
assert_type(s.str.split("a", expand=False), "pd.Series[list[str]]"), pd.Series
pan-vlados marked this conversation as resolved.
Show resolved Hide resolved
)
check(assert_type(s.str.startswith("a"), "pd.Series[bool]"), pd.Series, np.bool_)
check(
assert_type(s.str.startswith(("a", "b")), "pd.Series[bool]"),
Expand Down