Skip to content

TYP: assorted #43216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions pandas/_libs/algos.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ from typing import Any

import numpy as np

from pandas._typing import npt

class Infinity:
"""
Provide a positive Infinity comparison method for ranking.
Expand Down Expand Up @@ -30,7 +32,7 @@ class NegInfinity:
def unique_deltas(
arr: np.ndarray, # const int64_t[:]
) -> np.ndarray: ... # np.ndarray[np.int64, ndim=1]
def is_lexsorted(list_of_arrays: list[np.ndarray]) -> bool: ...
def is_lexsorted(list_of_arrays: list[npt.NDArray[np.int64]]) -> bool: ...
def groupsort_indexer(
index: np.ndarray, # const int64_t[:]
ngroups: int,
Expand Down Expand Up @@ -146,18 +148,18 @@ def diff_2d(
axis: int,
datetimelike: bool = ...,
) -> None: ...
def ensure_platform_int(arr: object) -> np.ndarray: ...
def ensure_object(arr: object) -> np.ndarray: ...
def ensure_float64(arr: object, copy=True) -> np.ndarray: ...
def ensure_float32(arr: object, copy=True) -> np.ndarray: ...
def ensure_int8(arr: object, copy=True) -> np.ndarray: ...
def ensure_int16(arr: object, copy=True) -> np.ndarray: ...
def ensure_int32(arr: object, copy=True) -> np.ndarray: ...
def ensure_int64(arr: object, copy=True) -> np.ndarray: ...
def ensure_uint8(arr: object, copy=True) -> np.ndarray: ...
def ensure_uint16(arr: object, copy=True) -> np.ndarray: ...
def ensure_uint32(arr: object, copy=True) -> np.ndarray: ...
def ensure_uint64(arr: object, copy=True) -> np.ndarray: ...
def ensure_platform_int(arr: object) -> npt.NDArray[np.intp]: ...
def ensure_object(arr: object) -> npt.NDArray[np.object_]: ...
def ensure_float64(arr: object, copy=True) -> npt.NDArray[np.float64]: ...
def ensure_float32(arr: object, copy=True) -> npt.NDArray[np.float32]: ...
def ensure_int8(arr: object, copy=True) -> npt.NDArray[np.int8]: ...
def ensure_int16(arr: object, copy=True) -> npt.NDArray[np.int16]: ...
def ensure_int32(arr: object, copy=True) -> npt.NDArray[np.int32]: ...
def ensure_int64(arr: object, copy=True) -> npt.NDArray[np.int64]: ...
def ensure_uint8(arr: object, copy=True) -> npt.NDArray[np.uint8]: ...
def ensure_uint16(arr: object, copy=True) -> npt.NDArray[np.uint16]: ...
def ensure_uint32(arr: object, copy=True) -> npt.NDArray[np.uint32]: ...
def ensure_uint64(arr: object, copy=True) -> npt.NDArray[np.uint64]: ...
def take_1d_int8_int8(
values: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=...
) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cpdef ndarray[int64_t, ndim=1] unique_deltas(const int64_t[:] arr):

Parameters
----------
arr : ndarray[in64_t]
arr : ndarray[int64_t]

Returns
-------
Expand Down
6 changes: 4 additions & 2 deletions pandas/_libs/hashing.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy as np

from pandas._typing import npt

def hash_object_array(
arr: np.ndarray, # np.ndarray[object]
arr: npt.NDArray[np.object_],
key: str,
encoding: str = ...,
) -> np.ndarray: ... # np.ndarray[np.uint64]
) -> npt.NDArray[np.uint64]: ...
17 changes: 8 additions & 9 deletions pandas/_libs/index.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np

from pandas._typing import npt

class IndexEngine:
over_size_threshold: bool
def __init__(self, vgetter, n: int): ...
Expand All @@ -16,21 +18,18 @@ class IndexEngine:
def is_monotonic_decreasing(self) -> bool: ...
def get_backfill_indexer(
self, other: np.ndarray, limit: int | None = ...
) -> np.ndarray: ...
) -> npt.NDArray[np.intp]: ...
def get_pad_indexer(
self, other: np.ndarray, limit: int | None = ...
) -> np.ndarray: ...
) -> npt.NDArray[np.intp]: ...
@property
def is_mapping_populated(self) -> bool: ...
def clear_mapping(self): ...
def get_indexer(self, values: np.ndarray) -> np.ndarray: ... # np.ndarray[np.intp]
def get_indexer(self, values: np.ndarray) -> npt.NDArray[np.intp]: ...
def get_indexer_non_unique(
self,
targets: np.ndarray,
) -> tuple[
np.ndarray, # np.ndarray[np.intp]
np.ndarray, # np.ndarray[np.intp]
]: ...
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...

class Float64Engine(IndexEngine): ...
class Float32Engine(IndexEngine): ...
Expand Down Expand Up @@ -58,8 +57,8 @@ class BaseMultiIndexCodesEngine:
): ...
def get_indexer(
self,
target: np.ndarray, # np.ndarray[object]
) -> np.ndarray: ... # np.ndarray[np.intp]
target: npt.NDArray[np.object_],
) -> npt.NDArray[np.intp]: ...
def _extract_level_codes(self, target: object): ...
def get_indexer_with_fill(
self,
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def unique(values):
unique1d = unique


def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]:
"""
Compute the isin boolean array.

Expand Down Expand Up @@ -903,7 +903,7 @@ def value_counts_arraylike(values, dropna: bool):

def duplicated(
values: ArrayLike, keep: Literal["first", "last", False] = "first"
) -> np.ndarray:
) -> npt.NDArray[np.bool_]:
"""
Return boolean ndarray denoting duplicate values.

Expand Down Expand Up @@ -1032,8 +1032,8 @@ def rank(
def checked_add_with_arr(
arr: np.ndarray,
b,
arr_mask: np.ndarray | None = None,
b_mask: np.ndarray | None = None,
arr_mask: npt.NDArray[np.bool_] | None = None,
b_mask: npt.NDArray[np.bool_] | None = None,
) -> np.ndarray:
"""
Perform array addition that checks for underflow and overflow.
Expand Down
13 changes: 7 additions & 6 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
DtypeObj,
NpDtype,
PositionalIndexer2D,
npt,
)
from pandas.compat.numpy import function as nv
from pandas.errors import (
Expand Down Expand Up @@ -274,7 +275,7 @@ def __iter__(self):
return (self._box_func(v) for v in self.asi8)

@property
def asi8(self) -> np.ndarray:
def asi8(self) -> npt.NDArray[np.int64]:
"""
Integer representation of the values.

Expand Down Expand Up @@ -774,7 +775,7 @@ def map(self, mapper):

return Index(self).map(mapper).array

def isin(self, values) -> np.ndarray:
def isin(self, values) -> npt.NDArray[np.bool_]:
"""
Compute boolean array of whether each value is found in the
passed set of values.
Expand Down Expand Up @@ -830,11 +831,11 @@ def isin(self, values) -> np.ndarray:
# ------------------------------------------------------------------
# Null Handling

def isna(self) -> np.ndarray:
def isna(self) -> npt.NDArray[np.bool_]:
return self._isnan

@property # NB: override with cache_readonly in immutable subclasses
def _isnan(self) -> np.ndarray:
def _isnan(self) -> npt.NDArray[np.bool_]:
"""
return if each value is nan
"""
Expand Down Expand Up @@ -1535,7 +1536,7 @@ class DatelikeOps(DatetimeLikeArrayMixin):
URL="https://docs.python.org/3/library/datetime.html"
"#strftime-and-strptime-behavior"
)
def strftime(self, date_format: str) -> np.ndarray:
def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
"""
Convert to Index using specified date_format.

Expand All @@ -1551,7 +1552,7 @@ def strftime(self, date_format: str) -> np.ndarray:

Returns
-------
ndarray
ndarray[object]
NumPy ndarray of formatted strings.

See Also
Expand Down
11 changes: 6 additions & 5 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
to_offset,
tzconversion,
)
from pandas._typing import npt
from pandas.errors import PerformanceWarning

from pandas.core.dtypes.cast import astype_dt64_to_dt64tz
Expand Down Expand Up @@ -656,7 +657,7 @@ def astype(self, dtype, copy: bool = True):
@dtl.ravel_compat
def _format_native_types(
self, na_rep="NaT", date_format=None, **kwargs
) -> np.ndarray:
) -> npt.NDArray[np.object_]:
from pandas.io.formats.format import get_format_datetime64_from_values

fmt = get_format_datetime64_from_values(self, date_format)
Expand Down Expand Up @@ -1045,7 +1046,7 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise") -> DatetimeArr
# ----------------------------------------------------------------
# Conversion Methods - Vectorized analogues of Timestamp methods

def to_pydatetime(self) -> np.ndarray:
def to_pydatetime(self) -> npt.NDArray[np.object_]:
"""
Return Datetime Array/Index as object ndarray of datetime.datetime
objects.
Expand Down Expand Up @@ -1262,7 +1263,7 @@ def day_name(self, locale=None):
return result

@property
def time(self) -> np.ndarray:
def time(self) -> npt.NDArray[np.object_]:
"""
Returns numpy array of datetime.time. The time part of the Timestamps.
"""
Expand All @@ -1274,15 +1275,15 @@ def time(self) -> np.ndarray:
return ints_to_pydatetime(timestamps, box="time")

@property
def timetz(self) -> np.ndarray:
def timetz(self) -> npt.NDArray[np.object_]:
"""
Returns numpy array of datetime.time also containing timezone
information. The time part of the Timestamps.
"""
return ints_to_pydatetime(self.asi8, self.tz, box="time")

@property
def date(self) -> np.ndarray:
def date(self) -> npt.NDArray[np.object_]:
"""
Returns numpy array of python datetime.date objects (namely, the date
part of Timestamps without timezone information).
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,7 @@ def agg_series(
out = npvalues
return out

def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray:
# -> np.ndarray[object]

def _aggregate_series_fast(self, obj: Series, func: F) -> npt.NDArray[np.object_]:
# At this point we have already checked that
# - obj.index is not a MultiIndex
# - obj is backed by an ndarray, not ExtensionArray
Expand All @@ -967,8 +965,9 @@ def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray:
return result

@final
def _aggregate_series_pure_python(self, obj: Series, func: F) -> np.ndarray:
# -> np.ndarray[object]
def _aggregate_series_pure_python(
self, obj: Series, func: F
) -> npt.NDArray[np.object_]:
ids, _, ngroups = self.group_info

counts = np.zeros(ngroups, dtype=int)
Expand Down Expand Up @@ -1190,12 +1189,12 @@ def __init__(
assert isinstance(axis, int), axis

@cache_readonly
def slabels(self) -> np.ndarray: # np.ndarray[np.intp]
def slabels(self) -> npt.NDArray[np.intp]:
# Sorted labels
return self.labels.take(self._sort_idx)

@cache_readonly
def _sort_idx(self) -> np.ndarray: # np.ndarray[np.intp]
def _sort_idx(self) -> npt.NDArray[np.intp]:
# Counting sort indexer
return get_group_index_sorter(self.labels, self.ngroups)

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4450,7 +4450,9 @@ def _get_leaf_sorter(labels: list[np.ndarray]) -> npt.NDArray[np.intp]:
return join_index, left_indexer, right_indexer

@final
def _join_monotonic(self, other: Index, how: str_t = "left"):
def _join_monotonic(
self, other: Index, how: str_t = "left"
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
# We only get here with matching dtypes
assert other.dtype == self.dtype

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pandas._typing import (
Dtype,
DtypeObj,
npt,
)
from pandas.util._decorators import (
cache_readonly,
Expand Down Expand Up @@ -411,7 +412,7 @@ class IntegerIndex(NumericIndex):
_is_backward_compat_public_numeric_index: bool = False

@property
def asi8(self) -> np.ndarray:
def asi8(self) -> npt.NDArray[np.int64]:
# do not cache or you'll create a memory leak
warnings.warn(
"Index.asi8 is deprecated and will be removed in a future version.",
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ArrayLike,
Axis,
F,
npt,
)
from pandas.compat._optional import import_optional_dependency

Expand Down Expand Up @@ -57,7 +58,7 @@ def check_value_size(value, mask: np.ndarray, length: int):
return value


def mask_missing(arr: ArrayLike, values_to_mask) -> np.ndarray:
def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]:
"""
Return a masking array of same size/shape as arr
with entries equaling any member of values_to_mask set to True
Expand Down
Loading