Skip to content

Commit 390e5d9

Browse files
committed
feat(index): append
1 parent f340905 commit 390e5d9

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,12 @@ class Index(IndexOpsMixin[S1]):
401401
) -> Self: ...
402402
@overload
403403
def __getitem__(self, idx: int | tuple[np_ndarray_anyint, ...]) -> S1: ...
404-
def append(self, other): ...
404+
@overload
405+
def append(self, other: Index[Never]) -> Index: ...
406+
@overload
407+
def append(self, other: Index[S1] | Sequence[Index[S1]]) -> Index[S1]: ...
408+
@overload
409+
def append(self, other: Index | Sequence) -> Index: ...
405410
def putmask(self, mask, value): ...
406411
def equals(self, other) -> bool: ...
407412
@final

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mypy = "1.17.0"
3939
pandas = "2.3.0"
4040
pyarrow = ">=10.0.1"
4141
pytest = ">=7.1.2"
42-
pyright = ">=1.1.400"
42+
pyright = ">=1.1.403"
4343
ty = "^0.0.1a8"
4444
pyrefly = "^0.21.0"
4545
poethepoet = ">=0.16.5"

tests/test_indexes.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,49 @@ def test_getitem() -> None:
10281028
check(assert_type(i0[[0, 2]], "pd.Index[str]"), pd.Index, str)
10291029

10301030

1031+
def test_append_any() -> None:
1032+
"""Test pd.Index.append that gives pd.Index[Any]"""
1033+
first = pd.Index([1])
1034+
second = pd.Index(["a"])
1035+
third = pd.Index([1, "a"])
1036+
check(assert_type(first.append(second), pd.Index), pd.Index)
1037+
check(assert_type(first.append([second]), pd.Index), pd.Index)
1038+
1039+
check(assert_type(first.append(third), pd.Index), pd.Index)
1040+
check(assert_type(first.append([third]), pd.Index), pd.Index)
1041+
check(assert_type(first.append([second, third]), pd.Index), pd.Index)
1042+
1043+
check(assert_type(third.append([]), "pd.Index[str | int]"), pd.Index) # type: ignore[assert-type]
1044+
check(assert_type(third.append([first]), pd.Index), pd.Index)
1045+
1046+
1047+
def test_append_int() -> None:
1048+
"""Test pd.Index[int].append"""
1049+
first = pd.Index([1])
1050+
second = pd.Index([2])
1051+
check(assert_type(first.append([]), "pd.Index[int]"), pd.Index, np.int64)
1052+
check(assert_type(first.append(second), "pd.Index[int]"), pd.Index, np.int64)
1053+
check(assert_type(first.append([second]), "pd.Index[int]"), pd.Index, np.int64)
1054+
1055+
1056+
def test_append_str() -> None:
1057+
"""Test pd.Index[str].append"""
1058+
first = pd.Index(["str"])
1059+
second = pd.Index(["rts"])
1060+
check(assert_type(first.append([]), "pd.Index[str]"), pd.Index, str)
1061+
check(assert_type(first.append(second), "pd.Index[str]"), pd.Index, str)
1062+
check(assert_type(first.append([second]), "pd.Index[str]"), pd.Index, str)
1063+
1064+
1065+
def test_append_list_str() -> None:
1066+
"""Test pd.Index[list[str]].append"""
1067+
first = pd.Index([["str", "rts"]])
1068+
second = pd.Index([["srt", "trs"]])
1069+
check(assert_type(first.append([]), "pd.Index[list[str]]"), pd.Index, list)
1070+
check(assert_type(first.append(second), "pd.Index[list[str]]"), pd.Index, list)
1071+
check(assert_type(first.append([second]), "pd.Index[list[str]]"), pd.Index, list)
1072+
1073+
10311074
def test_range_index_range() -> None:
10321075
"""Test that pd.RangeIndex can be initialized from range."""
10331076
iri = pd.RangeIndex(range(5))

0 commit comments

Comments
 (0)