|
10 | 10 | Hashable,
|
11 | 11 | List,
|
12 | 12 | Optional,
|
| 13 | + Sequence, |
| 14 | + TypeVar, |
13 | 15 | Union,
|
14 | 16 | )
|
15 | 17 | import warnings
|
|
22 | 24 | from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp
|
23 | 25 | from pandas._libs.tslibs.period import IncompatibleFrequency
|
24 | 26 | from pandas._libs.tslibs.timezones import tz_compare
|
25 |
| -from pandas._typing import DtypeObj, Label |
| 27 | +from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label |
26 | 28 | from pandas.compat import set_function_name
|
27 | 29 | from pandas.compat.numpy import function as nv
|
28 | 30 | from pandas.errors import InvalidIndexError
|
|
98 | 100 | )
|
99 | 101 |
|
100 | 102 | if TYPE_CHECKING:
|
101 |
| - from pandas import Series |
| 103 | + from pandas import RangeIndex, Series |
102 | 104 |
|
103 | 105 |
|
104 | 106 | __all__ = ["Index"]
|
@@ -188,6 +190,9 @@ def _new_Index(cls, d):
|
188 | 190 | return cls.__new__(cls, **d)
|
189 | 191 |
|
190 | 192 |
|
| 193 | +_IndexT = TypeVar("_IndexT", bound="Index") |
| 194 | + |
| 195 | + |
191 | 196 | class Index(IndexOpsMixin, PandasObject):
|
192 | 197 | """
|
193 | 198 | Immutable ndarray implementing an ordered, sliceable set. The basic object
|
@@ -787,7 +792,13 @@ def repeat(self, repeats, axis=None):
|
787 | 792 | # --------------------------------------------------------------------
|
788 | 793 | # Copying Methods
|
789 | 794 |
|
790 |
| - def copy(self, name=None, deep=False, dtype=None, names=None): |
| 795 | + def copy( |
| 796 | + self: _IndexT, |
| 797 | + name: Optional[Label] = None, |
| 798 | + deep: bool = False, |
| 799 | + dtype: Optional[Dtype] = None, |
| 800 | + names: Optional[Sequence[Label]] = None, |
| 801 | + ) -> _IndexT: |
791 | 802 | """
|
792 | 803 | Make a copy of this object.
|
793 | 804 |
|
@@ -949,10 +960,9 @@ def _format_with_header(
|
949 | 960 | # could have nans
|
950 | 961 | mask = isna(values)
|
951 | 962 | if mask.any():
|
952 |
| - result = np.array(result) |
953 |
| - result[mask] = na_rep |
954 |
| - # error: "List[str]" has no attribute "tolist" |
955 |
| - result = result.tolist() # type: ignore[attr-defined] |
| 963 | + result_arr = np.array(result) |
| 964 | + result_arr[mask] = na_rep |
| 965 | + result = result_arr.tolist() |
956 | 966 | else:
|
957 | 967 | result = trim_front(format_array(values, None, justify="left"))
|
958 | 968 | return header + result
|
@@ -4913,7 +4923,13 @@ def _get_string_slice(self, key: str_t, use_lhs: bool = True, use_rhs: bool = Tr
|
4913 | 4923 | # overridden in DatetimeIndex, TimedeltaIndex and PeriodIndex
|
4914 | 4924 | raise NotImplementedError
|
4915 | 4925 |
|
4916 |
| - def slice_indexer(self, start=None, end=None, step=None, kind=None): |
| 4926 | + def slice_indexer( |
| 4927 | + self, |
| 4928 | + start: Optional[Label] = None, |
| 4929 | + end: Optional[Label] = None, |
| 4930 | + step: Optional[int] = None, |
| 4931 | + kind: Optional[str_t] = None, |
| 4932 | + ) -> slice: |
4917 | 4933 | """
|
4918 | 4934 | Compute the slice indexer for input labels and step.
|
4919 | 4935 |
|
@@ -5513,7 +5529,9 @@ def ensure_index_from_sequences(sequences, names=None):
|
5513 | 5529 | return MultiIndex.from_arrays(sequences, names=names)
|
5514 | 5530 |
|
5515 | 5531 |
|
5516 |
| -def ensure_index(index_like, copy: bool = False): |
| 5532 | +def ensure_index( |
| 5533 | + index_like: Union[AnyArrayLike, Sequence], copy: bool = False |
| 5534 | +) -> Index: |
5517 | 5535 | """
|
5518 | 5536 | Ensure that we have an index from some index-like object.
|
5519 | 5537 |
|
@@ -5549,7 +5567,18 @@ def ensure_index(index_like, copy: bool = False):
|
5549 | 5567 | index_like = index_like.copy()
|
5550 | 5568 | return index_like
|
5551 | 5569 | if hasattr(index_like, "name"):
|
5552 |
| - return Index(index_like, name=index_like.name, copy=copy) |
| 5570 | + # https://github.com/python/mypy/issues/1424 |
| 5571 | + # error: Item "ExtensionArray" of "Union[ExtensionArray, |
| 5572 | + # Sequence[Any]]" has no attribute "name" [union-attr] |
| 5573 | + # error: Item "Sequence[Any]" of "Union[ExtensionArray, Sequence[Any]]" |
| 5574 | + # has no attribute "name" [union-attr] |
| 5575 | + # error: "Sequence[Any]" has no attribute "name" [attr-defined] |
| 5576 | + # error: Item "Sequence[Any]" of "Union[Series, Sequence[Any]]" has no |
| 5577 | + # attribute "name" [union-attr] |
| 5578 | + # error: Item "Sequence[Any]" of "Union[Any, Sequence[Any]]" has no |
| 5579 | + # attribute "name" [union-attr] |
| 5580 | + name = index_like.name # type: ignore[union-attr, attr-defined] |
| 5581 | + return Index(index_like, name=name, copy=copy) |
5553 | 5582 |
|
5554 | 5583 | if is_iterator(index_like):
|
5555 | 5584 | index_like = list(index_like)
|
@@ -5604,7 +5633,7 @@ def _validate_join_method(method: str):
|
5604 | 5633 | raise ValueError(f"do not recognize join method {method}")
|
5605 | 5634 |
|
5606 | 5635 |
|
5607 |
| -def default_index(n): |
| 5636 | +def default_index(n: int) -> "RangeIndex": |
5608 | 5637 | from pandas.core.indexes.range import RangeIndex
|
5609 | 5638 |
|
5610 | 5639 | return RangeIndex(0, n, name=None)
|
|
0 commit comments