From 4898d119a710ea64c9bfcfb4257d9d621aad893a Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 1 May 2021 09:37:38 -0700 Subject: [PATCH] TYP: assorted annotations --- pandas/_libs/hashtable.pyx | 2 +- pandas/_libs/indexing.pyx | 5 +++-- pandas/_libs/internals.pyx | 4 +++- pandas/core/frame.py | 1 - pandas/core/indexes/base.py | 12 +++++++++--- pandas/core/internals/managers.py | 4 ++-- pandas/core/reshape/reshape.py | 8 ++++++-- pandas/core/series.py | 6 +++--- pandas/core/window/rolling.py | 7 ++++--- 9 files changed, 31 insertions(+), 18 deletions(-) diff --git a/pandas/_libs/hashtable.pyx b/pandas/_libs/hashtable.pyx index 1e2a336f12444..4566f22be2c36 100644 --- a/pandas/_libs/hashtable.pyx +++ b/pandas/_libs/hashtable.pyx @@ -163,7 +163,7 @@ cdef class Int64Factorizer: @cython.wraparound(False) @cython.boundscheck(False) -def unique_label_indices(const int64_t[:] labels): +def unique_label_indices(const int64_t[:] labels) -> ndarray: """ Indices of the first occurrences of the unique labels *excluding* -1. equivalent to: diff --git a/pandas/_libs/indexing.pyx b/pandas/_libs/indexing.pyx index 7966fe8d4f045..bdbaa05138072 100644 --- a/pandas/_libs/indexing.pyx +++ b/pandas/_libs/indexing.pyx @@ -3,9 +3,10 @@ cdef class NDFrameIndexerBase: A base class for _NDFrameIndexer for fast instantiation and attribute access. """ cdef public: - object obj, name, _ndim + str name + object obj, _ndim - def __init__(self, name, obj): + def __init__(self, name: str, obj): self.obj = obj self.name = name self._ndim = None diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index f3bc70ad8a26b..77bb462c6df4a 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -372,7 +372,9 @@ cdef slice indexer_as_slice(intp_t[:] vals): @cython.boundscheck(False) @cython.wraparound(False) -def get_blkno_indexers(int64_t[:] blknos, bint group=True): +def get_blkno_indexers( + int64_t[:] blknos, bint group=True +) -> list[tuple[int, slice | np.ndarray]]: """ Enumerate contiguous runs of integers in ndarray. diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4469869a5a929..69e88cd221f6d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6219,7 +6219,6 @@ def sort_values( # type: ignore[override] indexer = lexsort_indexer( keys, orders=ascending, na_position=na_position, key=key ) - indexer = ensure_platform_int(indexer) elif len(by): by = by[0] diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 1bae9947bd875..7779335bfd3ba 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3414,7 +3414,7 @@ def get_indexer( limit: int | None = None, tolerance=None, ) -> np.ndarray: - + # returned ndarray is np.intp method = missing.clean_reindex_fill_method(method) target = ensure_index(target) @@ -4099,7 +4099,10 @@ def _join_multi(self, other: Index, how: str_t): return result @final - def _join_non_unique(self, other, how="left"): + def _join_non_unique( + self, other: Index, how: str_t = "left" + ) -> tuple[Index, np.ndarray, np.ndarray]: + # returned ndarrays are np.intp from pandas.core.reshape.merge import get_join_indexers # We only get here if dtypes match @@ -4125,7 +4128,10 @@ def _join_non_unique(self, other, how="left"): return join_index, left_idx, right_idx @final - def _join_level(self, other, level, how="left", keep_order=True): + def _join_level( + self, other: Index, level, how: str_t = "left", keep_order: bool = True + ) -> tuple[MultiIndex, np.ndarray | None, np.ndarray | None]: + # Any returned ndarrays are np.intp """ The join method *only* affects the level of the resulting MultiIndex. Otherwise it just exactly aligns the Index data to the diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index eac2579e7dfa5..73f463997c085 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1470,9 +1470,9 @@ def _interleave( if isinstance(dtype, SparseDtype): dtype = dtype.subtype elif isinstance(dtype, ExtensionDtype): - dtype = "object" + dtype = np.dtype("object") elif is_dtype_equal(dtype, str): - dtype = "object" + dtype = np.dtype("object") # error: Argument "dtype" to "empty" has incompatible type # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index 1a4d8dbe2885e..d1e076da9293d 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -131,7 +131,12 @@ def __init__(self, index: MultiIndex, level=-1, constructor=None): self._make_selectors() @cache_readonly - def _indexer_and_to_sort(self): + def _indexer_and_to_sort( + self, + ) -> tuple[ + np.ndarray, # np.ndarray[np.intp] + list[np.ndarray], # each has _some_ signed integer dtype + ]: v = self.level codes = list(self.index.codes) @@ -143,7 +148,6 @@ def _indexer_and_to_sort(self): ngroups = len(obs_ids) indexer = get_group_index_sorter(comp_index, ngroups) - indexer = ensure_platform_int(indexer) return indexer, to_sort @cache_readonly diff --git a/pandas/core/series.py b/pandas/core/series.py index 6a3e997a58754..ed9ce848678a0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -113,15 +113,15 @@ from pandas.core.indexes.accessors import CombinedDatetimelikeProperties from pandas.core.indexes.api import ( CategoricalIndex, + DatetimeIndex, Float64Index, Index, MultiIndex, + PeriodIndex, + TimedeltaIndex, ensure_index, ) import pandas.core.indexes.base as ibase -from pandas.core.indexes.datetimes import DatetimeIndex -from pandas.core.indexes.period import PeriodIndex -from pandas.core.indexes.timedeltas import TimedeltaIndex from pandas.core.indexing import check_bool_indexer from pandas.core.internals import ( SingleArrayManager, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 7c867487e92b0..1c85385c587a5 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -381,10 +381,11 @@ def _apply_series( """ obj = self._create_data(self._selected_obj) - try: + if name == "count": # GH 12541: Special case for count where we support date-like types - input = obj.values if name != "count" else notna(obj.values).astype(int) - values = self._prep_values(input) + obj = notna(obj).astype(int) + try: + values = self._prep_values(obj._values) except (TypeError, NotImplementedError) as err: raise DataError("No numeric types to aggregate") from err