Skip to content

Commit 7b43a93

Browse files
committed
GH1074 Add type hint Series[list[str]] for Series.str.split with expand=False
1 parent 63dfe96 commit 7b43a93

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

pandas-stubs/_typing.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ S1 = TypeVar(
547547
| Period
548548
| Interval
549549
| CategoricalDtype
550-
| BaseOffset,
550+
| BaseOffset
551+
| list[str],
551552
)
552553

553554
S2 = TypeVar(
@@ -566,7 +567,8 @@ S2 = TypeVar(
566567
| Period
567568
| Interval
568569
| CategoricalDtype
569-
| BaseOffset,
570+
| BaseOffset
571+
| list[str],
570572
)
571573

572574
IndexingInt: TypeAlias = (

pandas-stubs/core/series.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ class Series(IndexOpsMixin[S1], NDFrame):
242242
copy: bool = ...,
243243
) -> Series[float]: ...
244244
@overload
245-
def __new__( # type: ignore[overload-overlap]
245+
def __new__(
246246
cls,
247-
data: Sequence[Never],
247+
data: Sequence[str],
248248
index: Axes | None = ...,
249249
*,
250250
dtype: Dtype = ...,
251251
name: Hashable = ...,
252252
copy: bool = ...,
253-
) -> Series[Any]: ...
253+
) -> Series[str]: ...
254254
@overload
255255
def __new__(
256256
cls,

pandas-stubs/core/strings.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM]):
6565
self, pat: str = ..., *, n: int = ..., expand: Literal[True], regex: bool = ...
6666
) -> _TS: ...
6767
@overload
68+
def split(
69+
self, pat: str = ..., *, n: int = ..., expand: Literal[False], regex: bool = ...
70+
) -> Series[list[str]]: ...
71+
@overload
6872
def split(
6973
self, pat: str = ..., *, n: int = ..., expand: bool = ..., regex: bool = ...
7074
) -> T: ...

tests/test_frame.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3767,9 +3767,9 @@ class MyDict(TypedDict):
37673767

37683768

37693769
def test_series_empty_dtype() -> None:
3770-
"""Test for the creation of a Series from an empty list GH571 to map to a Series[Any]."""
3770+
"""Test for the creation of a Series from an empty list GH571 to map to a Series[str]."""
37713771
new_tab: Sequence[Never] = [] # need to be typehinted to please mypy
3772-
check(assert_type(pd.Series(new_tab), "pd.Series[Any]"), pd.Series)
3773-
check(assert_type(pd.Series([]), "pd.Series[Any]"), pd.Series)
3772+
check(assert_type(pd.Series(new_tab), "pd.Series[str]"), pd.Series)
3773+
check(assert_type(pd.Series([]), "pd.Series[str]"), pd.Series)
37743774
# ensure that an empty string does not get matched to Sequence[Never]
37753775
check(assert_type(pd.Series(""), "pd.Series[str]"), pd.Series)

tests/test_series.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,9 @@ def test_string_accessors():
15561556
check(assert_type(s.str.split("a"), pd.Series), pd.Series)
15571557
# GH 194
15581558
check(assert_type(s.str.split("a", expand=True), pd.DataFrame), pd.DataFrame)
1559+
check(
1560+
assert_type(s.str.split("a", expand=False), "pd.Series[list[str]]"), pd.Series
1561+
)
15591562
check(assert_type(s.str.startswith("a"), "pd.Series[bool]"), pd.Series, np.bool_)
15601563
check(
15611564
assert_type(s.str.startswith(("a", "b")), "pd.Series[bool]"),

0 commit comments

Comments
 (0)