Skip to content

Commit 667cef3

Browse files
committed
Add type hints and tests for str.rsplit() for expand=False
1 parent 7ba4a45 commit 667cef3

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

pandas-stubs/core/strings.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM, _TS2]):
7979
@overload
8080
def rsplit(self, pat: str = ..., *, n: int = ..., expand: Literal[True]) -> _TS: ...
8181
@overload
82-
def rsplit(self, pat: str = ..., *, n: int = ..., expand: bool = ...) -> T: ...
82+
def rsplit(
83+
self, pat: str = ..., *, n: int = ..., expand: Literal[False] = ...
84+
) -> _TS2: ...
8385
@overload
8486
def partition(self, sep: str = ...) -> pd.DataFrame: ...
8587
@overload

tests/test_indexes.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_difference_none() -> None:
111111
def test_str_split() -> None:
112112
# GH 194
113113
ind = pd.Index(["a-b", "c-d"])
114-
check(assert_type(ind.str.split("-"), "pd.Index[list[str]]"), pd.Index)
114+
check(assert_type(ind.str.split("-"), "pd.Index[list[str]]"), pd.Index, list)
115115
check(assert_type(ind.str.split("-", expand=True), pd.MultiIndex), pd.MultiIndex)
116116
check(
117117
assert_type(ind.str.split("-", expand=False), "pd.Index[list[str]]"),
@@ -120,6 +120,18 @@ def test_str_split() -> None:
120120
)
121121

122122

123+
def test_str_rsplit() -> None:
124+
# GH 1074
125+
ind = pd.Index(["a-b", "c-d"])
126+
check(assert_type(ind.str.rsplit("-"), "pd.Index[list[str]]"), pd.Index, list)
127+
check(assert_type(ind.str.rsplit("-", expand=True), pd.MultiIndex), pd.MultiIndex)
128+
check(
129+
assert_type(ind.str.rsplit("-", expand=False), "pd.Index[list[str]]"),
130+
pd.Index,
131+
list,
132+
)
133+
134+
123135
def test_str_match() -> None:
124136
i = pd.Index(
125137
["applep", "bananap", "Cherryp", "DATEp", "eGGpLANTp", "123p", "23.45p"]

tests/test_series.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,13 @@ def test_string_accessors():
15481548
check(assert_type(s.str.rindex("p"), pd.Series), pd.Series)
15491549
check(assert_type(s.str.rjust(80), pd.Series), pd.Series)
15501550
check(assert_type(s.str.rpartition("p"), pd.DataFrame), pd.DataFrame)
1551-
check(assert_type(s.str.rsplit("a"), pd.Series), pd.Series)
1551+
check(assert_type(s.str.rsplit("a"), "pd.Series[list[str]]"), pd.Series, list)
15521552
check(assert_type(s.str.rsplit("a", expand=True), pd.DataFrame), pd.DataFrame)
1553+
check(
1554+
assert_type(s.str.rsplit("a", expand=False), "pd.Series[list[str]]"),
1555+
pd.Series,
1556+
list,
1557+
)
15531558
check(assert_type(s.str.rstrip(), pd.Series), pd.Series)
15541559
check(assert_type(s.str.slice(0, 4, 2), pd.Series), pd.Series)
15551560
check(assert_type(s.str.slice_replace(0, 2, "XX"), pd.Series), pd.Series)

0 commit comments

Comments
 (0)