Skip to content

Commit 25de5ee

Browse files
Typing improvements for Index
1 parent 39a3bf3 commit 25de5ee

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

pandas/core/indexes/base.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
ClassVar,
1212
Literal,
1313
NoReturn,
14+
Optional,
15+
Union,
1416
cast,
1517
final,
1618
overload,
@@ -46,6 +48,7 @@
4648
Axes,
4749
Axis,
4850
DropKeep,
51+
Dtype,
4952
DtypeObj,
5053
F,
5154
IgnoreRaise,
@@ -155,6 +158,8 @@
155158
ExtensionArray,
156159
TimedeltaArray,
157160
)
161+
from pandas.core.arrays.floating import FloatingDtype
162+
from pandas.core.arrays.integer import IntegerDtype
158163
from pandas.core.arrays.string_ import (
159164
StringArray,
160165
StringDtype,
@@ -312,6 +317,20 @@ def _new_Index(cls, d):
312317
return cls.__new__(cls, **d)
313318

314319

320+
slice_type = Optional[
321+
Union[
322+
str,
323+
IntegerDtype,
324+
FloatingDtype,
325+
DatetimeTZDtype,
326+
CategoricalDtype,
327+
PeriodDtype,
328+
IntervalDtype,
329+
abc.Hashable,
330+
]
331+
]
332+
333+
315334
class Index(IndexOpsMixin, PandasObject):
316335
"""
317336
Immutable sequence used for indexing and alignment.
@@ -1087,7 +1106,7 @@ def view(self, cls=None):
10871106
result._id = self._id
10881107
return result
10891108

1090-
def astype(self, dtype, copy: bool = True):
1109+
def astype(self, dtype: Dtype, copy: bool = True):
10911110
"""
10921111
Create an Index with values cast to dtypes.
10931112
@@ -2957,7 +2976,7 @@ def _dti_setop_align_tzs(self, other: Index, setop: str_t) -> tuple[Index, Index
29572976
return self, other
29582977

29592978
@final
2960-
def union(self, other, sort=None):
2979+
def union(self, other, sort: bool | None = None):
29612980
"""
29622981
Form the union of two Index objects.
29632982
@@ -3334,7 +3353,7 @@ def _intersection_via_get_indexer(
33343353
return result
33353354

33363355
@final
3337-
def difference(self, other, sort=None):
3356+
def difference(self, other, sort: bool | None = None):
33383357
"""
33393358
Return a new Index with elements of index not in `other`.
33403359
@@ -3420,7 +3439,12 @@ def _wrap_difference_result(self, other, result):
34203439
# We will override for MultiIndex to handle empty results
34213440
return self._wrap_setop_result(other, result)
34223441

3423-
def symmetric_difference(self, other, result_name=None, sort=None):
3442+
def symmetric_difference(
3443+
self,
3444+
other,
3445+
result_name: abc.Hashable | None = None,
3446+
sort: bool | None = None,
3447+
):
34243448
"""
34253449
Compute the symmetric difference of two Index objects.
34263450
@@ -6389,7 +6413,7 @@ def _transform_index(self, func, *, level=None) -> Index:
63896413
items = [func(x) for x in self]
63906414
return Index(items, name=self.name, tupleize_cols=False)
63916415

6392-
def isin(self, values, level=None) -> npt.NDArray[np.bool_]:
6416+
def isin(self, values, level: np.str_ | int | None = None) -> npt.NDArray[np.bool_]:
63936417
"""
63946418
Return a boolean array where the index values are in `values`.
63956419
@@ -6687,7 +6711,12 @@ def get_slice_bound(self, label, side: Literal["left", "right"]) -> int:
66876711
else:
66886712
return slc
66896713

6690-
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
6714+
def slice_locs(
6715+
self,
6716+
start: slice_type = None,
6717+
end: slice_type = None,
6718+
step: int | None = None,
6719+
) -> tuple[int, int]:
66916720
"""
66926721
Compute slice locations for input labels.
66936722
@@ -6781,7 +6810,7 @@ def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
67816810

67826811
return start_slice, end_slice
67836812

6784-
def delete(self, loc) -> Self:
6813+
def delete(self, loc: int | list[int] | np.ndarray) -> Self:
67856814
"""
67866815
Make new Index with passed location(-s) deleted.
67876816
@@ -7227,7 +7256,9 @@ def _maybe_disable_logical_methods(self, opname: str_t) -> None:
72277256
raise TypeError(f"cannot perform {opname} with {type(self).__name__}")
72287257

72297258
@Appender(IndexOpsMixin.argmin.__doc__)
7230-
def argmin(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
7259+
def argmin(
7260+
self, axis: int | None = None, skipna: bool = True, *args, **kwargs
7261+
) -> int:
72317262
nv.validate_argmin(args, kwargs)
72327263
nv.validate_minmax_axis(axis)
72337264

@@ -7240,7 +7271,9 @@ def argmin(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
72407271
return super().argmin(skipna=skipna)
72417272

72427273
@Appender(IndexOpsMixin.argmax.__doc__)
7243-
def argmax(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
7274+
def argmax(
7275+
self, axis: int | None = None, skipna: bool = True, *args, **kwargs
7276+
) -> int:
72447277
nv.validate_argmax(args, kwargs)
72457278
nv.validate_minmax_axis(axis)
72467279

@@ -7251,7 +7284,7 @@ def argmax(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
72517284
raise ValueError("Encountered all NA values")
72527285
return super().argmax(skipna=skipna)
72537286

7254-
def min(self, axis=None, skipna: bool = True, *args, **kwargs):
7287+
def min(self, axis: int | None = None, skipna: bool = True, *args, **kwargs):
72557288
"""
72567289
Return the minimum value of the Index.
72577290
@@ -7314,7 +7347,7 @@ def min(self, axis=None, skipna: bool = True, *args, **kwargs):
73147347

73157348
return nanops.nanmin(self._values, skipna=skipna)
73167349

7317-
def max(self, axis=None, skipna: bool = True, *args, **kwargs):
7350+
def max(self, axis: int | None = None, skipna: bool = True, *args, **kwargs):
73187351
"""
73197352
Return the maximum value of the Index.
73207353

0 commit comments

Comments
 (0)