From e381dbe66f8d36fe281f9a8b4d2860824fa44817 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 1 Apr 2021 19:40:48 -0700 Subject: [PATCH 1/9] TYP: shared_docs --- pandas/core/arrays/datetimes.py | 12 ++++++------ pandas/core/arrays/interval.py | 2 +- pandas/core/arrays/period.py | 9 ++++----- pandas/core/generic.py | 4 ++-- pandas/core/groupby/base.py | 10 +++++++--- pandas/core/indexes/base.py | 4 ++-- pandas/core/indexes/category.py | 4 +++- pandas/core/indexing.py | 6 +++--- pandas/core/nanops.py | 24 ++++++++++++------------ pandas/core/ops/__init__.py | 4 ++-- pandas/core/sorting.py | 2 +- 11 files changed, 43 insertions(+), 38 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 956a93a142afe..baef3a1867822 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -192,7 +192,7 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): _infer_matches = ("datetime", "datetime64", "date") # define my properties & methods for delegation - _bool_ops = [ + _bool_ops: list[str] = [ "is_month_start", "is_month_end", "is_quarter_start", @@ -201,8 +201,8 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): "is_year_end", "is_leap_year", ] - _object_ops = ["freq", "tz"] - _field_ops = [ + _object_ops: list[str] = ["freq", "tz"] + _field_ops: list[str] = [ "year", "month", "day", @@ -222,9 +222,9 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): "microsecond", "nanosecond", ] - _other_ops = ["date", "time", "timetz"] - _datetimelike_ops = _field_ops + _object_ops + _bool_ops + _other_ops - _datetimelike_methods = [ + _other_ops: list[str] = ["date", "time", "timetz"] + _datetimelike_ops: list[str] = _field_ops + _object_ops + _bool_ops + _other_ops + _datetimelike_methods: list[str] = [ "to_period", "tz_localize", "tz_convert", diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 1cc0465121335..437642a2540c3 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -89,7 +89,7 @@ IntervalArrayT = TypeVar("IntervalArrayT", bound="IntervalArray") -_interval_shared_docs = {} +_interval_shared_docs: dict[str, str] = {} _shared_docs_kwargs = { "klass": "IntervalArray", diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 2355999933a7a..3ed190bb7aa23 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -6,7 +6,6 @@ TYPE_CHECKING, Any, Callable, - List, Optional, Sequence, Type, @@ -160,10 +159,10 @@ class PeriodArray(PeriodMixin, dtl.DatelikeOps): _infer_matches = ("period",) # Names others delegate to us - _other_ops: List[str] = [] - _bool_ops = ["is_leap_year"] - _object_ops = ["start_time", "end_time", "freq"] - _field_ops = [ + _other_ops: list[str] = [] + _bool_ops: list[str] = ["is_leap_year"] + _object_ops: list[str] = ["start_time", "end_time", "freq"] + _field_ops: list[str] = [ "year", "month", "day", diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6b4e3c7caef50..cffd1b8d5c27d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1822,9 +1822,9 @@ def _drop_labels_or_levels(self, keys, axis: int = 0): Parameters ---------- - keys: str or list of str + keys : str or list of str labels or levels to drop - axis: int, default 0 + axis : int, default 0 Axis that levels are associated with (0 for index, 1 for columns) Returns diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 927eb8eed8454..04ec48f129f41 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -106,12 +106,16 @@ def _gotitem(self, key, ndim, subset=None): | plotting_methods ) -series_apply_allowlist = ( +series_apply_allowlist: frozenset[str] = ( common_apply_allowlist - | {"nlargest", "nsmallest", "is_monotonic_increasing", "is_monotonic_decreasing"} + | frozenset( + {"nlargest", "nsmallest", "is_monotonic_increasing", "is_monotonic_decreasing"} + ) ) | frozenset(["dtype", "unique"]) -dataframe_apply_allowlist = common_apply_allowlist | frozenset(["dtypes", "corrwith"]) +dataframe_apply_allowlist: frozenset[str] = common_apply_allowlist | frozenset( + ["dtypes", "corrwith"] +) # cythonized transformations or canned "agg+broadcast", which do not # require postprocessing of the result by transform. diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 5163c55036fd0..4af40df279dfa 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -179,7 +179,7 @@ _unsortable_types = frozenset(("mixed", "mixed-integer")) -_index_doc_kwargs = { +_index_doc_kwargs: dict[str, str] = { "klass": "Index", "inplace": "", "target_klass": "Index", @@ -187,7 +187,7 @@ "unique": "Index", "duplicated": "np.ndarray", } -_index_shared_docs = {} +_index_shared_docs: dict[str, str] = {} str_t = str diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index c9c39fde1da46..1105a180dd09e 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import ( Any, Hashable, @@ -50,7 +52,7 @@ inherit_names, ) -_index_doc_kwargs = dict(ibase._index_doc_kwargs) +_index_doc_kwargs: dict[str, str] = dict(ibase._index_doc_kwargs) _index_doc_kwargs.update({"target_klass": "CategoricalIndex"}) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 4c8a6a200b196..ccf6ea5233315 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1062,7 +1062,7 @@ def _getitem_iterable(self, key, axis: int): ---------- key : iterable Targeted labels. - axis: int + axis : int Dimension on which the indexing is being made. Raises @@ -1262,7 +1262,7 @@ def _get_listlike_indexer(self, key, axis: int, raise_missing: bool = False): ---------- key : list-like Targeted labels. - axis: int + axis: int Dimension on which the indexing is being made. raise_missing: bool, default False Whether to raise a KeyError if some labels were not found. @@ -1317,7 +1317,7 @@ def _validate_read_indexer( indexer: array-like of booleans Indices corresponding to the key, (with -1 indicating not found). - axis: int + axis : int Dimension on which the indexing is being made. raise_missing: bool Whether to raise a KeyError if some labels are not found. Will be diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index de324561de030..ff19b39a0ae05 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -509,7 +509,7 @@ def nanall( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -552,7 +552,7 @@ def nansum( Parameters ---------- values : ndarray[dtype] - axis: int, optional + axis : int, optional skipna : bool, default True min_count: int, default 0 mask : ndarray[bool], optional @@ -627,7 +627,7 @@ def nanmean( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -683,7 +683,7 @@ def nanmedian(values, *, axis=None, skipna=True, mask=None): Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -840,7 +840,7 @@ def nanstd(values, *, axis=None, skipna=True, ddof=1, mask=None): Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is N - ddof, @@ -880,7 +880,7 @@ def nanvar(values, *, axis=None, skipna=True, ddof=1, mask=None): Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is N - ddof, @@ -955,7 +955,7 @@ def nansem( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is N - ddof, @@ -1036,7 +1036,7 @@ def nanargmax( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -1082,7 +1082,7 @@ def nanargmin( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -1134,7 +1134,7 @@ def nanskew( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -1221,7 +1221,7 @@ def nankurt( Parameters ---------- values : ndarray - axis: int, optional + axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known @@ -1312,7 +1312,7 @@ def nanprod( Parameters ---------- values : ndarray[dtype] - axis: int, optional + axis : int, optional skipna : bool, default True min_count: int, default 0 mask : ndarray[bool], optional diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 4ebcd6533af2e..e44615a5419f5 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -216,8 +216,8 @@ def align_method_FRAME( ---------- left : DataFrame right : Any - axis: int, str, or None - flex: bool or None, default False + axis : int, str, or None + flex : bool or None, default False Whether this is a flex op, in which case we reindex. None indicates not to check for alignment. level : int or level name, default None diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index 816c1d9195778..9d2fba823cfd6 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -407,7 +407,7 @@ def nargminmax(values, method: str, axis: int = 0): ---------- values : ExtensionArray method : {"argmax", "argmin"} - axis: int, default 0 + axis : int, default 0 Returns ------- From a4e6e4fbbba5008744f2fce63e8dff8211b38c97 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 1 Apr 2021 19:49:19 -0700 Subject: [PATCH 2/9] TYP: Index constructors --- pandas/core/indexes/category.py | 8 ++++---- pandas/core/indexes/datetimes.py | 33 ++++++++++++++++---------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 1105a180dd09e..59d780450a5c7 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -219,9 +219,9 @@ def __new__( categories=None, ordered=None, dtype: Optional[Dtype] = None, - copy=False, - name=None, - ): + copy: bool = False, + name: Hashable = None, + ) -> CategoricalIndex: name = maybe_extract_name(name, data, cls) @@ -241,7 +241,7 @@ def _shallow_copy( self, values: Categorical, name: Hashable = no_default, - ): + ) -> CategoricalIndex: name = self._name if name is no_default else name if values is not None: diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 0b80e863ef3ea..c4285b530c1a7 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -10,6 +10,7 @@ import operator from typing import ( TYPE_CHECKING, + Hashable, Optional, Tuple, ) @@ -318,15 +319,15 @@ def __new__( data=None, freq=lib.no_default, tz=None, - normalize=False, + normalize: bool = False, closed=None, ambiguous="raise", - dayfirst=False, - yearfirst=False, + dayfirst: bool = False, + yearfirst: bool = False, dtype: Optional[Dtype] = None, - copy=False, - name=None, - ): + copy: bool = False, + name: Hashable = None, + ) -> DatetimeIndex: if is_scalar(data): raise TypeError( @@ -641,7 +642,7 @@ def _validate_partial_date_slice(self, reso: Resolution): # _parsed_string_to_bounds allows it. raise KeyError - def _deprecate_mismatched_indexing(self, key): + def _deprecate_mismatched_indexing(self, key) -> None: # GH#36148 # we get here with isinstance(key, self._data._recognized_scalars) try: @@ -855,7 +856,7 @@ def inferred_type(self) -> str: # sure we can't have ambiguous indexing return "datetime64" - def indexer_at_time(self, time, asof=False): + def indexer_at_time(self, time, asof: bool = False) -> np.ndarray: """ Return index locations of values at particular time of day (e.g. 9:30AM). @@ -869,7 +870,7 @@ def indexer_at_time(self, time, asof=False): Returns ------- - values_at_time : array of integers + np.ndarray[np.intp] See Also -------- @@ -895,8 +896,8 @@ def indexer_at_time(self, time, asof=False): return (micros == time_micros).nonzero()[0] def indexer_between_time( - self, start_time, end_time, include_start=True, include_end=True - ): + self, start_time, end_time, include_start: bool = True, include_end: bool = True + ) -> np.ndarray: """ Return index locations of values between particular times of day (e.g., 9:00-9:30AM). @@ -912,7 +913,7 @@ def indexer_between_time( Returns ------- - values_between_time : array of integers + np.ndarray[np.intp] See Also -------- @@ -952,8 +953,8 @@ def date_range( periods=None, freq=None, tz=None, - normalize=False, - name=None, + normalize: bool = False, + name: Hashable = None, closed=None, **kwargs, ) -> DatetimeIndex: @@ -1124,8 +1125,8 @@ def bdate_range( periods: Optional[int] = None, freq="B", tz=None, - normalize=True, - name=None, + normalize: bool = True, + name: Hashable = None, weekmask=None, holidays=None, closed=None, From 483d7273d2d2df4e3795d81976fe8f8da71efae0 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 1 Apr 2021 20:12:48 -0700 Subject: [PATCH 3/9] TYP: indexes --- pandas/core/indexes/extension.py | 4 ++-- pandas/core/indexes/interval.py | 20 ++++++++++---------- pandas/core/indexes/period.py | 12 +++++++----- pandas/core/indexes/range.py | 14 +++++++------- pandas/tests/reshape/concat/test_append.py | 5 ++++- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 76c16dee1cda1..b11ec06120e0c 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -351,7 +351,7 @@ def map(self, mapper, na_action=None): return self.astype(object).map(mapper) @doc(Index.astype) - def astype(self, dtype, copy=True): + def astype(self, dtype, copy: bool = True) -> Index: dtype = pandas_dtype(dtype) if is_dtype_equal(self.dtype, dtype): if not copy: @@ -410,7 +410,7 @@ def _simple_new( def _get_engine_target(self) -> np.ndarray: return self._data._ndarray - def insert(self: _T, loc: int, item) -> _T: + def insert(self: _T, loc: int, item) -> Index: """ Make new Index inserting new item at location. Follows Python list.append semantics for negative values. diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 9bfc21a940917..103de5b2bcf46 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -287,7 +287,7 @@ def __new__( copy: bool = False, name: Hashable = None, verify_integrity: bool = True, - ): + ) -> IntervalIndex: name = maybe_extract_name(name, data, cls) @@ -322,10 +322,10 @@ def from_breaks( cls, breaks, closed: str = "right", - name=None, + name: Hashable = None, copy: bool = False, dtype: Optional[Dtype] = None, - ): + ) -> IntervalIndex: with rewrite_exception("IntervalArray", cls.__name__): array = IntervalArray.from_breaks( breaks, closed=closed, copy=copy, dtype=dtype @@ -353,10 +353,10 @@ def from_arrays( left, right, closed: str = "right", - name=None, + name: Hashable = None, copy: bool = False, dtype: Optional[Dtype] = None, - ): + ) -> IntervalIndex: with rewrite_exception("IntervalArray", cls.__name__): array = IntervalArray.from_arrays( left, right, closed, copy=copy, dtype=dtype @@ -383,10 +383,10 @@ def from_tuples( cls, data, closed: str = "right", - name=None, + name: Hashable = None, copy: bool = False, dtype: Optional[Dtype] = None, - ): + ) -> IntervalIndex: with rewrite_exception("IntervalArray", cls.__name__): arr = IntervalArray.from_tuples(data, closed=closed, copy=copy, dtype=dtype) return cls._simple_new(arr, name=name) @@ -882,7 +882,7 @@ def putmask(self, mask, value) -> Index: arr.putmask(mask, value) return type(self)._simple_new(arr, name=self.name) - def insert(self, loc, item): + def insert(self, loc: int, item): """ Return a new IntervalIndex inserting new item at location. Follows Python list.append semantics for negative values. Only Interval @@ -1081,8 +1081,8 @@ def _is_type_compatible(a, b) -> bool: def interval_range( - start=None, end=None, periods=None, freq=None, name=None, closed="right" -): + start=None, end=None, periods=None, freq=None, name: Hashable = None, closed="right" +) -> IntervalIndex: """ Return a fixed frequency IntervalIndex. diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 66f2b757438a7..a493e0c6160d7 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -6,6 +6,7 @@ ) from typing import ( Any, + Hashable, Optional, ) import warnings @@ -218,10 +219,10 @@ def __new__( ordinal=None, freq=None, dtype: Optional[Dtype] = None, - copy=False, - name=None, + copy: bool = False, + name: Hashable = None, **fields, - ): + ) -> PeriodIndex: valid_field_set = { "year", @@ -325,7 +326,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: # ------------------------------------------------------------------------ # Rendering Methods - def _mpl_repr(self): + def _mpl_repr(self) -> np.ndarray: # how to represent ourselves to matplotlib return self.astype(object)._values @@ -389,7 +390,8 @@ def __array_wrap__(self, result, context=None): def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray: """ where : array of timestamps - mask : array of booleans where data is not NA + mask : np.ndarray[bool] + Array of booleans where data is not NA. """ if isinstance(where, DatetimeIndex): where = PeriodIndex(where._values, freq=self.freq) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 7f2361eeb4d05..97fbf8c14c476 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -110,9 +110,9 @@ def __new__( stop=None, step=None, dtype: Optional[Dtype] = None, - copy=False, - name=None, - ): + copy: bool = False, + name: Hashable = None, + ) -> RangeIndex: # error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type # "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int], @@ -583,7 +583,7 @@ def _intersection(self, other: Index, sort=False): # solve intersection problem # performance hint: for identical step sizes, could use # cheaper alternative - gcd, s, t = self._extended_gcd(first.step, second.step) + gcd, s, _ = self._extended_gcd(first.step, second.step) # check whether element sets intersect if (first.start - second.start) % gcd: @@ -618,7 +618,7 @@ def _max_fitting_element(self, upper_limit: int) -> int: no_steps = (upper_limit - self.start) // abs(self.step) return self.start + abs(self.step) * no_steps - def _extended_gcd(self, a, b): + def _extended_gcd(self, a: int, b: int) -> tuple[int, int, int]: """ Extended Euclidean algorithms to solve Bezout's identity: a*x + b*y = gcd(x, y) @@ -744,7 +744,7 @@ def _difference(self, other, sort=None): new_index = new_index[::-1] return new_index - def symmetric_difference(self, other, result_name=None, sort=None): + def symmetric_difference(self, other, result_name: Hashable = None, sort=None): if not isinstance(other, RangeIndex) or sort is not None: return super().symmetric_difference(other, result_name, sort) @@ -758,7 +758,7 @@ def symmetric_difference(self, other, result_name=None, sort=None): # -------------------------------------------------------------------- - def _concat(self, indexes, name): + def _concat(self, indexes, name: Hashable): """ Overriding parent method for the case of all RangeIndex instances. diff --git a/pandas/tests/reshape/concat/test_append.py b/pandas/tests/reshape/concat/test_append.py index 7b9f8d1c2879e..c799f1c7a7660 100644 --- a/pandas/tests/reshape/concat/test_append.py +++ b/pandas/tests/reshape/concat/test_append.py @@ -188,7 +188,10 @@ def test_append_preserve_index_name(self): pd.MultiIndex.from_arrays(["A B C".split(), "D E F".split()]) ] - all_indexes = indexes_can_append + indexes_cannot_append_with_other + # error: Unsupported operand types for + ("List[Index]" and "List[MultiIndex]") + all_indexes = ( + indexes_can_append + indexes_cannot_append_with_other # type: ignore[operator] + ) @pytest.mark.parametrize("index", all_indexes, ids=lambda x: type(x).__name__) def test_append_same_columns_type(self, index): From 276e8098a96f077c8f9173035d921a03770e7fc4 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 1 Apr 2021 20:25:28 -0700 Subject: [PATCH 4/9] TYP: indexes.base --- pandas/core/indexes/base.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 4af40df279dfa..07b6b26810bcc 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1167,7 +1167,7 @@ def _format_with_header( return header + result @final - def to_native_types(self, slicer=None, **kwargs): + def to_native_types(self, slicer=None, **kwargs) -> np.ndarray: """ Format specified values of `self` and return them. @@ -4410,7 +4410,7 @@ def memory_usage(self, deep: bool = False) -> int: return result @final - def where(self, cond, other=None): + def where(self, cond, other=None) -> Index: """ Replace values where the condition is False. @@ -4626,7 +4626,7 @@ def _can_hold_identifiers_and_holds_name(self, name) -> bool: return name in self return False - def append(self, other): + def append(self, other) -> Index: """ Append a collection of Index options together. @@ -4636,7 +4636,7 @@ def append(self, other): Returns ------- - appended : Index + Index """ to_concat = [self] @@ -4866,7 +4866,7 @@ def asof(self, label): loc = loc.indices(len(self))[-1] return self[loc] - def asof_locs(self, where: Index, mask) -> np.ndarray: + def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray: """ Return the locations (indices) of labels in the index. @@ -4883,13 +4883,13 @@ def asof_locs(self, where: Index, mask) -> np.ndarray: ---------- where : Index An Index consisting of an array of timestamps. - mask : array-like + mask : np.ndarray[bool] Array of booleans denoting where values in the original data are not NA. Returns ------- - numpy.ndarray + np.ndarray[np.intp] An array of locations (indices) of the labels from the Index which correspond to the return values of the `asof` function for every element in `where`. @@ -4897,10 +4897,12 @@ def asof_locs(self, where: Index, mask) -> np.ndarray: locs = self._values[mask].searchsorted(where._values, side="right") locs = np.where(locs > 0, locs - 1, 0) - result = np.arange(len(self))[mask].take(locs) + result = np.arange(len(self), dtype=np.intp)[mask].take(locs) # TODO: overload return type of ExtensionArray.__getitem__ - first_value = cast(Any, self._values[mask.argmax()]) + # error: Invalid index type "signedinteger[Any]" for + # "Union[ExtensionArray, ndarray]"; expected type "Union[int, slice, ndarray]" + first_value = cast(Any, self._values[mask.argmax()]) # type: ignore[index] result[(locs == 0) & (where._values < first_value)] = -1 return result @@ -5070,7 +5072,7 @@ def argsort(self, *args, **kwargs) -> np.ndarray: Returns ------- - numpy.ndarray + np.ndarray[np.intp] Integer indices that would sort the index if used as an indexer. @@ -5858,7 +5860,7 @@ def delete(self, loc) -> Index: Returns ------- Index - New Index with passed location(-s) deleted. + Will be same type as self, except for RangeIndex. See Also -------- @@ -6017,7 +6019,7 @@ def __inv__(self): # TODO: __inv__ vs __invert__? return self._unary_method(lambda x: -x) - def any(self, *args, **kwargs): + def any(self, *args, **kwargs) -> bool: """ Return whether any element is Truthy. @@ -6059,9 +6061,9 @@ def any(self, *args, **kwargs): # "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, # float, complex, str, bytes, generic]], Sequence[Sequence[Any]], # _SupportsArray]" - return np.any(self.values) # type: ignore[arg-type] + return bool(np.any(self.values)) # type: ignore[arg-type] - def all(self, *args, **kwargs): + def all(self, *args, **kwargs) -> bool: """ Return whether all elements are Truthy. @@ -6120,7 +6122,7 @@ def all(self, *args, **kwargs): # "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, # float, complex, str, bytes, generic]], Sequence[Sequence[Any]], # _SupportsArray]" - return np.all(self.values) # type: ignore[arg-type] + return bool(np.all(self.values)) # type: ignore[arg-type] @final def _maybe_disable_logical_methods(self, opname: str_t): @@ -6374,8 +6376,8 @@ def _maybe_cast_data_without_dtype(subarr): elif inferred == "interval": try: - data = IntervalArray._from_sequence(subarr, copy=False) - return data + ia_data = IntervalArray._from_sequence(subarr, copy=False) + return ia_data except (ValueError, TypeError): # GH27172: mixed closed Intervals --> object dtype pass From 63ea1f1acdecb279f881cb0f9d6d5d708aefc7d7 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 1 Apr 2021 20:42:33 -0700 Subject: [PATCH 5/9] missing future import --- pandas/core/groupby/base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 04ec48f129f41..1755af9d35f74 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -3,6 +3,8 @@ hold the allowlist of methods that are exposed on the SeriesGroupBy and the DataFrameGroupBy objects. """ +from __future__ import annotations + import collections from typing import List From 5915671ee555efbfb4af69e7cd4bd1b65fea1744 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 2 Apr 2021 06:58:16 -0700 Subject: [PATCH 6/9] revert for any/all --- pandas/core/indexes/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 07b6b26810bcc..c6cdd4a435058 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6019,7 +6019,7 @@ def __inv__(self): # TODO: __inv__ vs __invert__? return self._unary_method(lambda x: -x) - def any(self, *args, **kwargs) -> bool: + def any(self, *args, **kwargs): """ Return whether any element is Truthy. @@ -6061,9 +6061,9 @@ def any(self, *args, **kwargs) -> bool: # "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, # float, complex, str, bytes, generic]], Sequence[Sequence[Any]], # _SupportsArray]" - return bool(np.any(self.values)) # type: ignore[arg-type] + return np.any(self.values) # type: ignore[arg-type] - def all(self, *args, **kwargs) -> bool: + def all(self, *args, **kwargs): """ Return whether all elements are Truthy. @@ -6122,7 +6122,7 @@ def all(self, *args, **kwargs) -> bool: # "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, # float, complex, str, bytes, generic]], Sequence[Sequence[Any]], # _SupportsArray]" - return bool(np.all(self.values)) # type: ignore[arg-type] + return np.all(self.values) # type: ignore[arg-type] @final def _maybe_disable_logical_methods(self, opname: str_t): From 4df2b99e880931bb4638ff5e70bd6ee492ecdbeb Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 8 Apr 2021 15:56:39 -0700 Subject: [PATCH 7/9] pre-commit fixup --- pandas/core/indexes/category.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 59d780450a5c7..b5089621313b8 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -3,8 +3,6 @@ from typing import ( Any, Hashable, - List, - Optional, ) import warnings @@ -218,7 +216,7 @@ def __new__( data=None, categories=None, ordered=None, - dtype: Optional[Dtype] = None, + dtype: Dtype | None = None, copy: bool = False, name: Hashable = None, ) -> CategoricalIndex: @@ -351,7 +349,7 @@ def _format_attrs(self): attrs.append(("length", len(self))) return attrs - def _format_with_header(self, header: List[str], na_rep: str = "NaN") -> List[str]: + def _format_with_header(self, header: list[str], na_rep: str = "NaN") -> list[str]: from pandas.io.formats.printing import pprint_thing result = [ @@ -424,10 +422,9 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): target = ibase.ensure_index(target) - missing: List[int] if self.equals(target): indexer = None - missing = [] + missing = np.array([], dtype=np.intp) else: indexer, missing = self.get_indexer_non_unique(np.array(target)) @@ -496,8 +493,8 @@ def _maybe_cast_indexer(self, key) -> int: def _get_indexer( self, target: Index, - method: Optional[str] = None, - limit: Optional[int] = None, + method: str | None = None, + limit: int | None = None, tolerance=None, ) -> np.ndarray: @@ -628,7 +625,7 @@ def map(self, mapper): mapped = self._values.map(mapper) return Index(mapped, name=self.name) - def _concat(self, to_concat: List[Index], name: Hashable) -> Index: + def _concat(self, to_concat: list[Index], name: Hashable) -> Index: # if calling index is category, don't check dtype of others try: codes = np.concatenate([self._is_dtype_compat(c).codes for c in to_concat]) From bb4b5f8f784bebd8ed8fef9cdab15d7cd55f8133 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 8 Apr 2021 16:27:49 -0700 Subject: [PATCH 8/9] pre-commit fixup --- pandas/core/groupby/base.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 1755af9d35f74..50248d5af8883 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -6,7 +6,6 @@ from __future__ import annotations import collections -from typing import List from pandas._typing import final @@ -21,7 +20,7 @@ class ShallowMixin(PandasObject): - _attributes: List[str] = [] + _attributes: list[str] = [] @final def _shallow_copy(self, obj, **kwargs): @@ -41,7 +40,7 @@ class GotItemMixin(PandasObject): Provide the groupby facilities to the mixed object. """ - _attributes: List[str] + _attributes: list[str] @final def _gotitem(self, key, ndim, subset=None): From f1195e796330bf4ee02f28bf2fc5695af19a2ce4 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 8 Apr 2021 18:00:19 -0700 Subject: [PATCH 9/9] mypy fixup --- pandas/core/indexes/base.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 03b029ae2b257..842b940f6cca3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4880,9 +4880,7 @@ def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray: result = np.arange(len(self), dtype=np.intp)[mask].take(locs) # TODO: overload return type of ExtensionArray.__getitem__ - # error: Invalid index type "signedinteger[Any]" for - # "Union[ExtensionArray, ndarray]"; expected type "Union[int, slice, ndarray]" - first_value = cast(Any, self._values[mask.argmax()]) # type: ignore[index] + first_value = cast(Any, self._values[mask.argmax()]) result[(locs == 0) & (where._values < first_value)] = -1 return result