Skip to content

Commit 8c1a120

Browse files
committed
Merge branch 'main' of github.com:pandas-dev/pandas-stubs into feature/reduce-ndarray
2 parents 0023b02 + 251b9ad commit 8c1a120

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ from pandas._typing import (
6969
T_COMPLEX,
7070
AnyAll,
7171
AnyArrayLike,
72+
AnyArrayLikeInt,
7273
ArrayLike,
7374
AxesData,
7475
CategoryDtypeArg,
@@ -408,7 +409,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
408409
notnull = ...
409410
def fillna(self, value=...): ...
410411
def dropna(self, how: AnyAll = "any") -> Self: ...
411-
def unique(self, level=...) -> Self: ...
412+
def unique(self, level: Hashable | None = None) -> Self: ...
412413
def drop_duplicates(self, *, keep: DropKeep = ...) -> Self: ...
413414
def duplicated(self, keep: DropKeep = "first") -> np_1darray[np.bool]: ...
414415
def __and__(self, other: Never) -> Never: ...
@@ -444,12 +445,12 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
444445
) -> np_1darray[np.intp]: ...
445446
def reindex(
446447
self,
447-
target,
448-
method: ReindexMethod | None = ...,
449-
level=...,
450-
limit=...,
451-
tolerance=...,
452-
): ...
448+
target: Iterable[Any],
449+
method: ReindexMethod | None = None,
450+
level: int | None = None,
451+
limit: int | None = None,
452+
tolerance: Scalar | AnyArrayLike | Sequence[Scalar] | None = None,
453+
) -> tuple[Index, np_1darray[np.intp] | None]: ...
453454
@overload
454455
def join(
455456
self,
@@ -485,7 +486,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
485486
cond: Sequence[bool] | np_ndarray_bool | BooleanArray | IndexOpsMixin[bool],
486487
other: Scalar | AnyArrayLike | None = None,
487488
) -> Index: ...
488-
def __contains__(self, key) -> bool: ...
489+
def __contains__(self, key: Hashable) -> bool: ...
489490
@final
490491
def __setitem__(self, key, value) -> None: ...
491492
@overload
@@ -502,7 +503,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
502503
@overload
503504
def append(self, other: Index | Sequence[Index]) -> Index: ...
504505
def putmask(self, mask, value): ...
505-
def equals(self, other) -> bool: ...
506+
def equals(self, other: object) -> bool: ...
506507
@final
507508
def identical(self, other) -> bool: ...
508509
@final
@@ -536,8 +537,13 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
536537
def slice_locs(
537538
self, start: SliceType = None, end: SliceType = None, step: int | None = None
538539
): ...
539-
def delete(self, loc) -> Self: ...
540-
def insert(self, loc, item) -> Self: ...
540+
def delete(
541+
self, loc: np.integer | int | AnyArrayLikeInt | Sequence[int]
542+
) -> Self: ...
543+
@overload
544+
def insert(self, loc: int, item: S1) -> Self: ...
545+
@overload
546+
def insert(self, loc: int, item: object) -> Index: ...
541547
def drop(self, labels, errors: IgnoreRaise = "raise") -> Self: ...
542548
@property
543549
def shape(self) -> tuple[int, ...]: ...

pandas-stubs/core/indexes/category.pyi

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ from collections.abc import (
22
Hashable,
33
Iterable,
44
)
5-
from typing import final
65

76
import numpy as np
87
from pandas.core.accessor import PandasDelegate
@@ -13,6 +12,8 @@ from typing_extensions import Self
1312

1413
from pandas._typing import (
1514
S1,
15+
Dtype,
16+
ListLike,
1617
np_1darray,
1718
)
1819

@@ -24,28 +25,20 @@ class CategoricalIndex(ExtensionIndex[S1], PandasDelegate):
2425
def __new__(
2526
cls,
2627
data: Iterable[S1] = ...,
27-
categories=...,
28-
ordered=...,
29-
dtype=...,
30-
copy: bool = ...,
31-
name: Hashable = ...,
28+
categories: ListLike | None = None,
29+
ordered: bool | None = None,
30+
dtype: Dtype | None = None,
31+
copy: bool = False,
32+
name: Hashable | None = None,
3233
) -> Self: ...
33-
def equals(self, other): ...
3434
@property
3535
def inferred_type(self) -> str: ...
3636
@property
37-
def values(self): ...
38-
def __contains__(self, key) -> bool: ...
39-
@property
4037
def is_unique(self) -> bool: ...
4138
@property
4239
def is_monotonic_increasing(self) -> bool: ...
4340
@property
4441
def is_monotonic_decreasing(self) -> bool: ...
45-
def unique(self, level=...): ...
46-
def reindex(self, target, method=..., level=..., limit=..., tolerance=...): ...
47-
@final
48-
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
49-
def get_indexer_non_unique(self, target): ...
50-
def delete(self, loc): ...
51-
def insert(self, loc, item): ...
42+
# `item` might be `S1` but not one of the categories, thus changing
43+
# the return type from `CategoricalIndex` to `Index`.
44+
def insert(self, loc: int, item: object) -> Index: ... # type: ignore[override]

pandas-stubs/core/indexes/range.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class RangeIndex(_IndexSubclassBase[int, np.int64]):
6161
def is_monotonic_decreasing(self) -> bool: ...
6262
@property
6363
def has_duplicates(self) -> bool: ...
64-
def __contains__(self, key: int | np.integer) -> bool: ...
6564
def factorize(
6665
self, sort: bool = False, use_na_sentinel: bool = True
6766
) -> tuple[np_1darray[np.intp], RangeIndex]: ...

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,6 @@ ignore = [
204204
"PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/
205205
"ERA001", "PLR0402", "PLC0105"
206206
]
207-
"*category.pyi" = [
208-
# TODO: remove when pandas-dev/pandas-stubs#1443 is resolved
209-
"ANN001", "ANN201", "ANN204", "ANN206",
210-
]
211207
"*series.pyi" = [
212208
# TODO: remove when pandas-dev/pandas-stubs#1444 is resolved
213209
"ANN001", "ANN201", "ANN204", "ANN206",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from __future__ import annotations
2+
3+
import numpy as np
4+
import pandas as pd
5+
from typing_extensions import (
6+
assert_type,
7+
)
8+
9+
from tests import (
10+
check,
11+
np_1darray,
12+
)
13+
14+
15+
def test_categoricalindex_unique() -> None:
16+
ci = pd.CategoricalIndex(["a", "b"])
17+
check(
18+
assert_type(ci.unique(), "pd.CategoricalIndex[str]"),
19+
pd.CategoricalIndex,
20+
)
21+
22+
23+
def test_categoricalindex_reindex() -> None:
24+
ci = pd.CategoricalIndex(["a", "b"])
25+
check(
26+
assert_type(ci.reindex([0, 1]), tuple[pd.Index, np_1darray[np.intp] | None]),
27+
tuple,
28+
)
29+
30+
31+
def test_categoricalindex_delete() -> None:
32+
ci = pd.CategoricalIndex(["a", "b"])
33+
check(assert_type(ci.delete(0), "pd.CategoricalIndex[str]"), pd.CategoricalIndex)
34+
check(
35+
assert_type(ci.delete([0, 1]), "pd.CategoricalIndex[str]"), pd.CategoricalIndex
36+
)
37+
38+
39+
def test_categoricalindex_insert() -> None:
40+
ci = pd.CategoricalIndex(["a", "b"])
41+
check(assert_type(ci.insert(0, "c"), pd.Index), pd.Index)

0 commit comments

Comments
 (0)