From 2e1126c230a91a3f86e7a056b488549f99440399 Mon Sep 17 00:00:00 2001 From: grace eze Date: Fri, 28 Jul 2023 20:46:06 +0200 Subject: [PATCH] Issue #5 done: typing improvements for Index --- pandas/core/indexes/base.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 45695e7324882..722dbe92e3af0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -51,6 +51,7 @@ Axis, AxisInt, DropKeep, + Dtype, DtypeObj, F, IgnoreRaise, @@ -1035,7 +1036,7 @@ def view(self, cls=None): result._id = self._id return result - def astype(self, dtype, copy: bool = True): + def astype(self, dtype: Dtype | None = None, copy: bool = True): """ Create an Index with values cast to dtypes. @@ -1229,7 +1230,7 @@ def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool: """ @Appender(_index_shared_docs["repeat"] % _index_doc_kwargs) - def repeat(self, repeats, axis=None): + def repeat(self, repeats, axis: AxisInt | None = None): repeats = ensure_platform_int(repeats) nv.validate_repeat((), {"axis": axis}) res_values = self._values.repeat(repeats) @@ -3216,7 +3217,7 @@ def _dti_setop_align_tzs(self, other: Index, setop: str_t) -> tuple[Index, Index return self, other @final - def union(self, other, sort=None): + def union(self, other, sort: bool | None = None): """ Form the union of two Index objects. @@ -3578,7 +3579,7 @@ def _intersection_via_get_indexer( return result @final - def difference(self, other, sort=None): + def difference(self, other, sort: bool | None = None): """ Return a new Index with elements of index not in `other`. @@ -3662,7 +3663,9 @@ def _wrap_difference_result(self, other, result): # We will override for MultiIndex to handle empty results return self._wrap_setop_result(other, result) - def symmetric_difference(self, other, result_name=None, sort=None): + def symmetric_difference( + self, other, result_name: str | None = None, sort: bool | None = None + ): """ Compute the symmetric difference of two Index objects. @@ -6450,7 +6453,7 @@ def _transform_index(self, func, *, level=None) -> Index: items = [func(x) for x in self] return Index(items, name=self.name, tupleize_cols=False) - def isin(self, values, level=None) -> npt.NDArray[np.bool_]: + def isin(self, values, level: str | int = None) -> npt.NDArray[np.bool_]: """ Return a boolean array where the index values are in `values`. @@ -6750,7 +6753,9 @@ def get_slice_bound(self, label, side: Literal["left", "right"]) -> int: else: return slc - def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]: + def slice_locs( + self, start: str | None = None, end: str | None = None, step: int | None = None + ) -> tuple[int, int]: """ Compute slice locations for input labels. @@ -6838,7 +6843,7 @@ def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]: return start_slice, end_slice - def delete(self, loc) -> Self: + def delete(self, loc: int | list[int]) -> Self: """ Make new Index with passed location(-s) deleted. @@ -7269,7 +7274,9 @@ def _maybe_disable_logical_methods(self, opname: str_t) -> None: make_invalid_op(opname)(self) @Appender(IndexOpsMixin.argmin.__doc__) - def argmin(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs) -> int: + def argmin( + self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs + ) -> int: nv.validate_argmin(args, kwargs) nv.validate_minmax_axis(axis) @@ -7288,7 +7295,9 @@ def argmin(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs return super().argmin(skipna=skipna) @Appender(IndexOpsMixin.argmax.__doc__) - def argmax(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs) -> int: + def argmax( + self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs + ) -> int: nv.validate_argmax(args, kwargs) nv.validate_minmax_axis(axis) @@ -7306,7 +7315,7 @@ def argmax(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs return -1 return super().argmax(skipna=skipna) - def min(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs): + def min(self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs): """ Return the minimum value of the Index. @@ -7369,7 +7378,7 @@ def min(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs): return nanops.nanmin(self._values, skipna=skipna) - def max(self, axis: AxisInt|None = None, skipna: bool = True, *args, **kwargs): + def max(self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs): """ Return the maximum value of the Index.