From c6fe0ee76aeac5803f456ee215cd7ddbd87c658d Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 6 Sep 2021 12:25:18 -0700 Subject: [PATCH] TYP: libs --- pandas/_libs/join.pyi | 36 +++++++++++++------------- pandas/_libs/lib.pyi | 43 ++++++++++++++++---------------- pandas/_libs/ops.pyi | 14 ++++++----- pandas/_libs/parsers.pyi | 3 ++- pandas/_libs/reshape.pyi | 6 +++-- pandas/_libs/tslib.pyi | 8 +++--- pandas/_libs/window/indexers.pyi | 4 ++- 7 files changed, 63 insertions(+), 51 deletions(-) diff --git a/pandas/_libs/join.pyi b/pandas/_libs/join.pyi index f73f495cf4d4f..5b0e50ca76840 100644 --- a/pandas/_libs/join.pyi +++ b/pandas/_libs/join.pyi @@ -1,51 +1,53 @@ import numpy as np +from pandas._typing import npt + def inner_join( left: np.ndarray, # const intp_t[:] right: np.ndarray, # const intp_t[:] max_groups: int, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def left_outer_join( left: np.ndarray, # const intp_t[:] right: np.ndarray, # const intp_t[:] max_groups: int, sort: bool = True, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def full_outer_join( left: np.ndarray, # const intp_t[:] right: np.ndarray, # const intp_t[:] max_groups: int, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def ffill_indexer( indexer: np.ndarray, # const intp_t[:] -) -> np.ndarray: ... # np.ndarray[np.intp] +) -> npt.NDArray[np.intp]: ... def left_join_indexer_unique( left: np.ndarray, # ndarray[join_t] right: np.ndarray, # ndarray[join_t] -) -> np.ndarray: ... # np.ndarray[np.intp] +) -> npt.NDArray[np.intp]: ... def left_join_indexer( left: np.ndarray, # ndarray[join_t] right: np.ndarray, # ndarray[join_t] ) -> tuple[ np.ndarray, # np.ndarray[join_t] - np.ndarray, # np.ndarray[np.intp] - np.ndarray, # np.ndarray[np.intp] + npt.NDArray[np.intp], + npt.NDArray[np.intp], ]: ... def inner_join_indexer( left: np.ndarray, # ndarray[join_t] right: np.ndarray, # ndarray[join_t] ) -> tuple[ np.ndarray, # np.ndarray[join_t] - np.ndarray, # np.ndarray[np.intp] - np.ndarray, # np.ndarray[np.intp] + npt.NDArray[np.intp], + npt.NDArray[np.intp], ]: ... def outer_join_indexer( left: np.ndarray, # ndarray[join_t] right: np.ndarray, # ndarray[join_t] ) -> tuple[ np.ndarray, # np.ndarray[join_t] - np.ndarray, # np.ndarray[np.intp] - np.ndarray, # np.ndarray[np.intp] + npt.NDArray[np.intp], + npt.NDArray[np.intp], ]: ... def asof_join_backward_on_X_by_Y( left_values: np.ndarray, # asof_t[:] @@ -54,7 +56,7 @@ def asof_join_backward_on_X_by_Y( right_by_values: np.ndarray, # by_t[:] allow_exact_matches: bool = True, tolerance=None, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_forward_on_X_by_Y( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] @@ -62,7 +64,7 @@ def asof_join_forward_on_X_by_Y( right_by_values: np.ndarray, # by_t[:] allow_exact_matches: bool = True, tolerance=None, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_nearest_on_X_by_Y( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] @@ -70,22 +72,22 @@ def asof_join_nearest_on_X_by_Y( right_by_values: np.ndarray, # by_t[:] allow_exact_matches: bool = True, tolerance=None, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_backward( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] allow_exact_matches: bool = True, tolerance=None, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_forward( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] allow_exact_matches: bool = True, tolerance=None, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_nearest( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] allow_exact_matches: bool = True, tolerance=None, -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.intp] # np.ndarray[np.intp] +) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... diff --git a/pandas/_libs/lib.pyi b/pandas/_libs/lib.pyi index 5be50f16af003..25fdec1bfca63 100644 --- a/pandas/_libs/lib.pyi +++ b/pandas/_libs/lib.pyi @@ -14,6 +14,7 @@ import numpy as np from pandas._typing import ( ArrayLike, DtypeObj, + npt, ) # placeholder until we can specify np.ndarray[object, ndim=2] @@ -63,7 +64,7 @@ def map_infer( ) -> np.ndarray: ... @overload # both convert_datetime and convert_to_nullable_integer False -> np.ndarray def maybe_convert_objects( - objects: np.ndarray, # np.ndarray[object] + objects: npt.NDArray[np.object_], *, try_float: bool = ..., safe: bool = ..., @@ -76,7 +77,7 @@ def maybe_convert_objects( ) -> np.ndarray: ... @overload def maybe_convert_objects( - objects: np.ndarray, # np.ndarray[object] + objects: npt.NDArray[np.object_], *, try_float: bool = ..., safe: bool = ..., @@ -89,7 +90,7 @@ def maybe_convert_objects( ) -> ArrayLike: ... @overload def maybe_convert_objects( - objects: np.ndarray, # np.ndarray[object] + objects: npt.NDArray[np.object_], *, try_float: bool = ..., safe: bool = ..., @@ -102,7 +103,7 @@ def maybe_convert_objects( ) -> ArrayLike: ... @overload def maybe_convert_objects( - objects: np.ndarray, # np.ndarray[object] + objects: npt.NDArray[np.object_], *, try_float: bool = ..., safe: bool = ..., @@ -115,7 +116,7 @@ def maybe_convert_objects( ) -> ArrayLike: ... @overload def maybe_convert_objects( - objects: np.ndarray, # np.ndarray[object] + objects: npt.NDArray[np.object_], *, try_float: bool = ..., safe: bool = ..., @@ -128,7 +129,7 @@ def maybe_convert_objects( ) -> ArrayLike: ... @overload def maybe_convert_numeric( - values: np.ndarray, # np.ndarray[object] + values: npt.NDArray[np.object_], na_values: set, convert_empty: bool = True, coerce_numeric: bool = False, @@ -136,7 +137,7 @@ def maybe_convert_numeric( ) -> tuple[np.ndarray, None]: ... @overload def maybe_convert_numeric( - values: np.ndarray, # np.ndarray[object] + values: npt.NDArray[np.object_], na_values: set, convert_empty: bool = True, coerce_numeric: bool = False, @@ -151,32 +152,32 @@ def ensure_string_array( convert_na_value: bool = True, copy: bool = True, skipna: bool = True, -) -> np.ndarray: ... # np.ndarray[object] +) -> npt.NDArray[np.object_]: ... def infer_datetimelike_array( - arr: np.ndarray, # np.ndarray[object] + arr: npt.NDArray[np.object_], ) -> tuple[str, bool]: ... def astype_intsafe( - arr: np.ndarray, # np.ndarray[object] + arr: npt.NDArray[np.object_], new_dtype: np.dtype, ) -> np.ndarray: ... -def fast_zip(ndarrays: list) -> np.ndarray: ... # np.ndarray[object] +def fast_zip(ndarrays: list) -> npt.NDArray[np.object_]: ... # TODO: can we be more specific about rows? def to_object_array_tuples(rows: object) -> ndarray_obj_2d: ... def tuples_to_object_array( - tuples: np.ndarray, # np.ndarray[object] + tuples: npt.NDArray[np.object_], ) -> ndarray_obj_2d: ... # TODO: can we be more specific about rows? def to_object_array(rows: object, min_width: int = 0) -> ndarray_obj_2d: ... def dicts_to_array(dicts: list, columns: list) -> ndarray_obj_2d: ... def maybe_booleans_to_slice( - mask: np.ndarray, # ndarray[uint8_t] -) -> slice | np.ndarray: ... # np.ndarray[np.uint8] + mask: npt.NDArray[np.uint8], +) -> slice | npt.NDArray[np.uint8]: ... def maybe_indices_to_slice( - indices: np.ndarray, # np.ndarray[np.intp] + indices: npt.NDArray[np.intp], max_len: int, -) -> slice | np.ndarray: ... # np.ndarray[np.uint8] +) -> slice | npt.NDArray[np.intp]: ... def is_all_arraylike(obj: list) -> bool: ... # ----------------------------------------------------------------- @@ -192,14 +193,14 @@ def map_infer_mask( dtype: np.dtype = ..., ) -> np.ndarray: ... def indices_fast( - index: np.ndarray, # ndarray[intp_t] + index: npt.NDArray[np.intp], labels: np.ndarray, # const int64_t[:] keys: list, - sorted_labels: list[np.ndarray], # list[ndarray[np.int64]] + sorted_labels: list[npt.NDArray[np.int64]], ) -> dict: ... def generate_slices( labels: np.ndarray, ngroups: int # const intp_t[:] -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.int64] # np.ndarray[np.int64] +) -> tuple[npt.NDArray[np.int64], npt.NDArray[np.int64]]: ... def count_level_2d( mask: np.ndarray, # ndarray[uint8_t, ndim=2, cast=True], labels: np.ndarray, # const intp_t[:] @@ -211,7 +212,7 @@ def get_level_sorter( starts: np.ndarray, # const intp_t[:] ) -> np.ndarray: ... # np.ndarray[np.intp, ndim=1] def generate_bins_dt64( - values: np.ndarray, # np.ndarray[np.int64] + values: npt.NDArray[np.int64], binner: np.ndarray, # const int64_t[:] closed: object = "left", hasnans: bool = False, @@ -224,5 +225,5 @@ def has_infs(arr: np.ndarray) -> bool: ... # const floating[:] def get_reverse_indexer( indexer: np.ndarray, # const intp_t[:] length: int, -) -> np.ndarray: ... # np.ndarray[np.intp] +) -> npt.NDArray[np.intp]: ... def is_bool_list(obj: list) -> bool: ... diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index d84b0dee20e7d..11ae3b852e97a 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -7,6 +7,8 @@ from typing import ( import numpy as np +from pandas._typing import npt + _BinOp = Callable[[Any, Any], Any] _BoolOp = Callable[[Any, Any], bool] @@ -14,12 +16,12 @@ def scalar_compare( values: np.ndarray, # object[:] val: object, op: _BoolOp, # {operator.eq, operator.ne, ...} -) -> np.ndarray: ... # np.ndarray[bool] +) -> npt.NDArray[np.bool_]: ... def vec_compare( - left: np.ndarray, # np.ndarray[object] - right: np.ndarray, # np.ndarray[object] + left: npt.NDArray[np.object_], + right: npt.NDArray[np.object_], op: _BoolOp, # {operator.eq, operator.ne, ...} -) -> np.ndarray: ... # np.ndarray[bool] +) -> npt.NDArray[np.bool_]: ... def scalar_binop( values: np.ndarray, # object[:] val: object, @@ -32,14 +34,14 @@ def vec_binop( ) -> np.ndarray: ... @overload def maybe_convert_bool( - arr: np.ndarray, # np.ndarray[object] + arr: npt.NDArray[np.object_], true_values=..., false_values=..., convert_to_masked_nullable: Literal[False] = ..., ) -> tuple[np.ndarray, None]: ... @overload def maybe_convert_bool( - arr: np.ndarray, # np.ndarray[object] + arr: npt.NDArray[np.object_], true_values=..., false_values=..., *, diff --git a/pandas/_libs/parsers.pyi b/pandas/_libs/parsers.pyi index 9ff05adceb2b4..9e3c163fb54d9 100644 --- a/pandas/_libs/parsers.pyi +++ b/pandas/_libs/parsers.pyi @@ -8,12 +8,13 @@ import numpy as np from pandas._typing import ( ArrayLike, Dtype, + npt, ) STR_NA_VALUES: set[str] def sanitize_objects( - values: np.ndarray, # ndarray[object] + values: npt.NDArray[np.object_], na_values: set, convert_empty: bool = ..., ) -> int: ... diff --git a/pandas/_libs/reshape.pyi b/pandas/_libs/reshape.pyi index 0457ceb1e03e6..893826a35d41e 100644 --- a/pandas/_libs/reshape.pyi +++ b/pandas/_libs/reshape.pyi @@ -1,5 +1,7 @@ import numpy as np +import pandas._tying as npt + def unstack( values: np.ndarray, # reshape_t[:, :] mask: np.ndarray, # const uint8_t[:] @@ -10,5 +12,5 @@ def unstack( new_mask: np.ndarray, # uint8_t[:, :] ) -> None: ... def explode( - values: np.ndarray, # np.ndarray[object] -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[object] # np.ndarray[np.int64] + values: npt.NDArray[np.object_], +) -> tuple[npt.NDArray[np.object_], npt.NDArray[np.int64]]: ... diff --git a/pandas/_libs/tslib.pyi b/pandas/_libs/tslib.pyi index f43a81f20700a..4b02235ac9925 100644 --- a/pandas/_libs/tslib.pyi +++ b/pandas/_libs/tslib.pyi @@ -2,19 +2,21 @@ from datetime import tzinfo import numpy as np +from pandas._typing import npt + def format_array_from_datetime( - values: np.ndarray, # np.ndarray[np.int64] + values: npt.NDArray[np.int64], tz: tzinfo | None = ..., format: str | None = ..., na_rep: object = ..., -) -> np.ndarray: ... # np.ndarray[object] +) -> npt.NDArray[np.object_]: ... def array_with_unit_to_datetime( values: np.ndarray, unit: str, errors: str = ..., ) -> tuple[np.ndarray, tzinfo | None]: ... def array_to_datetime( - values: np.ndarray, # np.ndarray[object] + values: npt.NDArray[np.object_], errors: str = ..., dayfirst: bool = ..., yearfirst: bool = ..., diff --git a/pandas/_libs/window/indexers.pyi b/pandas/_libs/window/indexers.pyi index 2dea9362228e5..c9bc64be34ac9 100644 --- a/pandas/_libs/window/indexers.pyi +++ b/pandas/_libs/window/indexers.pyi @@ -1,5 +1,7 @@ import numpy as np +from pandas._typing import npt + def calculate_variable_window_bounds( num_values: int, # int64_t window_size: int, # int64_t @@ -7,4 +9,4 @@ def calculate_variable_window_bounds( center: bool, closed: str | None, index: np.ndarray, # const int64_t[:] -) -> tuple[np.ndarray, np.ndarray,]: ... # np.ndarray[np.int64] # np.ndarray[np.int64] +) -> tuple[npt.NDArray[np.int64], npt.NDArray[np.int64]]: ...