|
49 | 49 | DtypeObj,
|
50 | 50 | NpDtype,
|
51 | 51 | PositionalIndexer2D,
|
| 52 | + PositionalIndexerTuple, |
| 53 | + ScalarIndexer, |
| 54 | + SequenceIndexer, |
52 | 55 | npt,
|
53 | 56 | )
|
54 | 57 | from pandas.compat.numpy import function as nv
|
@@ -313,17 +316,33 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
|
313 | 316 | return np.array(list(self), dtype=object)
|
314 | 317 | return self._ndarray
|
315 | 318 |
|
| 319 | + @overload |
| 320 | + def __getitem__(self, item: ScalarIndexer) -> DTScalarOrNaT: |
| 321 | + ... |
| 322 | + |
| 323 | + @overload |
316 | 324 | def __getitem__(
|
317 |
| - self, key: PositionalIndexer2D |
318 |
| - ) -> DatetimeLikeArrayMixin | DTScalarOrNaT: |
| 325 | + self: DatetimeLikeArrayT, |
| 326 | + item: SequenceIndexer | PositionalIndexerTuple, |
| 327 | + ) -> DatetimeLikeArrayT: |
| 328 | + ... |
| 329 | + |
| 330 | + def __getitem__( |
| 331 | + self: DatetimeLikeArrayT, key: PositionalIndexer2D |
| 332 | + ) -> DatetimeLikeArrayT | DTScalarOrNaT: |
319 | 333 | """
|
320 | 334 | This getitem defers to the underlying array, which by-definition can
|
321 | 335 | only handle list-likes, slices, and integer scalars
|
322 | 336 | """
|
323 |
| - result = super().__getitem__(key) |
| 337 | + # Use cast as we know we will get back a DatetimeLikeArray or DTScalar |
| 338 | + result = cast( |
| 339 | + Union[DatetimeLikeArrayT, DTScalarOrNaT], super().__getitem__(key) |
| 340 | + ) |
324 | 341 | if lib.is_scalar(result):
|
325 | 342 | return result
|
326 |
| - |
| 343 | + else: |
| 344 | + # At this point we know the result is an array. |
| 345 | + result = cast(DatetimeLikeArrayT, result) |
327 | 346 | result._freq = self._get_getitem_freq(key)
|
328 | 347 | return result
|
329 | 348 |
|
@@ -1768,11 +1787,7 @@ def factorize(self, na_sentinel=-1, sort: bool = False):
|
1768 | 1787 | uniques = self.copy() # TODO: copy or view?
|
1769 | 1788 | if sort and self.freq.n < 0:
|
1770 | 1789 | codes = codes[::-1]
|
1771 |
| - # TODO: overload __getitem__, a slice indexer returns same type as self |
1772 |
| - # error: Incompatible types in assignment (expression has type |
1773 |
| - # "Union[DatetimeLikeArrayMixin, Union[Any, Any]]", variable |
1774 |
| - # has type "TimelikeOps") |
1775 |
| - uniques = uniques[::-1] # type: ignore[assignment] |
| 1790 | + uniques = uniques[::-1] |
1776 | 1791 | return codes, uniques
|
1777 | 1792 | # FIXME: shouldn't get here; we are ignoring sort
|
1778 | 1793 | return super().factorize(na_sentinel=na_sentinel)
|
|
0 commit comments