Skip to content

Commit bf1221e

Browse files
authored
feat(index): append (#1282)
* feat(index): append * fix(comment): #1282 (comment) * fix(comment): https://github.com/pandas-dev/pandas-stubs/pull/1282/files#r2216273709 * fix(pyrefly): ignore * fix(comment): #1282 (comment) * fix(comment): #1282 (comment) * fix(comment): #1282 (comment) * fix: Sequence[Index] instead of Sequence * fix(comment): avoid union types #1282 (comment) * chore: #1282 (comment) * fix: constraint instead of bound to get rid of Any python/mypy#19525 (comment)
1 parent 845f9c5 commit bf1221e

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

pandas-stubs/_typing.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,26 @@ S1 = TypeVar("S1", bound=SeriesDType, default=Any)
847847
S2 = TypeVar("S2", bound=SeriesDType)
848848
S3 = TypeVar("S3", bound=SeriesDType)
849849

850+
# Constraint, instead of bound
851+
C2 = TypeVar(
852+
"C2",
853+
str,
854+
bytes,
855+
datetime.date,
856+
datetime.time,
857+
bool,
858+
int,
859+
float,
860+
complex,
861+
Dtype,
862+
datetime.datetime, # includes pd.Timestamp
863+
datetime.timedelta, # includes pd.Timedelta
864+
Period,
865+
Interval,
866+
CategoricalDtype,
867+
BaseOffset,
868+
)
869+
850870
IndexingInt: TypeAlias = (
851871
int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8
852872
)

pandas-stubs/core/indexes/base.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from typing_extensions import (
4242

4343
from pandas._libs.interval import _OrderableT
4444
from pandas._typing import (
45+
C2,
4546
S1,
4647
AnyAll,
4748
AxesData,
@@ -401,7 +402,12 @@ class Index(IndexOpsMixin[S1]):
401402
) -> Self: ...
402403
@overload
403404
def __getitem__(self, idx: int | tuple[np_ndarray_anyint, ...]) -> S1: ...
404-
def append(self, other): ...
405+
@overload
406+
def append(
407+
self: Index[C2], other: Index[C2] | Sequence[Index[C2]]
408+
) -> Index[C2]: ...
409+
@overload
410+
def append(self, other: Index | Sequence[Index]) -> Index: ...
405411
def putmask(self, mask, value): ...
406412
def equals(self, other) -> bool: ...
407413
@final

pandas-stubs/core/indexes/multi.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class MultiIndex(Index):
135135
def take(
136136
self, indices, axis: int = ..., allow_fill: bool = ..., fill_value=..., **kwargs
137137
): ...
138-
def append(self, other): ...
138+
def append(self, other): ... # pyrefly: ignore
139139
def argsort(self, *args, **kwargs): ...
140140
def repeat(self, repeats, axis=...): ...
141141
@final

tests/test_indexes.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import datetime as dt
44
from typing import (
55
TYPE_CHECKING,
6+
Any,
67
Union,
8+
cast,
79
)
810

911
import numpy as np
@@ -1028,6 +1030,42 @@ def test_getitem() -> None:
10281030
check(assert_type(i0[[0, 2]], "pd.Index[str]"), pd.Index, str)
10291031

10301032

1033+
def test_append_mix() -> None:
1034+
"""Test pd.Index.append with mixed types"""
1035+
first = pd.Index([1])
1036+
second = pd.Index(["a"])
1037+
third = pd.Index([1, "a"])
1038+
check(assert_type(first.append(second), pd.Index), pd.Index)
1039+
check(assert_type(first.append([second]), pd.Index), pd.Index)
1040+
1041+
check(assert_type(first.append(third), pd.Index), pd.Index)
1042+
check(assert_type(first.append([third]), pd.Index), pd.Index)
1043+
check(assert_type(first.append([second, third]), pd.Index), pd.Index)
1044+
1045+
check(assert_type(third.append([]), pd.Index), pd.Index)
1046+
check(assert_type(third.append(cast("list[Index[Any]]", [])), pd.Index), pd.Index)
1047+
check(assert_type(third.append([first]), pd.Index), pd.Index)
1048+
check(assert_type(third.append([first, second]), pd.Index), pd.Index)
1049+
1050+
1051+
def test_append_int() -> None:
1052+
"""Test pd.Index[int].append"""
1053+
first = pd.Index([1])
1054+
second = pd.Index([2])
1055+
check(assert_type(first.append([]), "pd.Index[int]"), pd.Index, np.int64)
1056+
check(assert_type(first.append(second), "pd.Index[int]"), pd.Index, np.int64)
1057+
check(assert_type(first.append([second]), "pd.Index[int]"), pd.Index, np.int64)
1058+
1059+
1060+
def test_append_str() -> None:
1061+
"""Test pd.Index[str].append"""
1062+
first = pd.Index(["str"])
1063+
second = pd.Index(["rts"])
1064+
check(assert_type(first.append([]), "pd.Index[str]"), pd.Index, str)
1065+
check(assert_type(first.append(second), "pd.Index[str]"), pd.Index, str)
1066+
check(assert_type(first.append([second]), "pd.Index[str]"), pd.Index, str)
1067+
1068+
10311069
def test_range_index_range() -> None:
10321070
"""Test that pd.RangeIndex can be initialized from range."""
10331071
iri = pd.RangeIndex(range(5))

0 commit comments

Comments
 (0)