Skip to content

TYP: assorted annotations #39798

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 10 commits into from
Feb 21, 2021
10 changes: 4 additions & 6 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ cdef class SeriesBinGrouper(_BaseGrouper):
ndarray arr, index, dummy_arr, dummy_index
object values, f, bins, typ, ityp, name

def __init__(self, object series, object f, object bins, object dummy):
def __init__(self, object series, object f, object bins):

assert dummy is not None # always obj[:0]
assert len(bins) > 0 # otherwise we get IndexError in get_result

self.bins = bins
Expand All @@ -127,6 +126,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
self.index = series.index.values
self.name = series.name

dummy = series.iloc[:0]
self.dummy_arr, self.dummy_index = self._check_dummy(dummy)

# kludge for #1688
Expand Down Expand Up @@ -203,10 +203,7 @@ cdef class SeriesGrouper(_BaseGrouper):
object f, labels, values, typ, ityp, name

def __init__(self, object series, object f, object labels,
Py_ssize_t ngroups, object dummy):

# in practice we always pass obj.iloc[:0] or equivalent
assert dummy is not None
Py_ssize_t ngroups):

if len(series) == 0:
# get_result would never assign `result`
Expand All @@ -225,6 +222,7 @@ cdef class SeriesGrouper(_BaseGrouper):
self.index = series.index.values
self.name = series.name

dummy = series.iloc[:0]
self.dummy_arr, self.dummy_index = self._check_dummy(dummy)
self.ngroups = ngroups

Expand Down
54 changes: 39 additions & 15 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def _reconstruct_data(
return values


def _ensure_arraylike(values):
def _ensure_arraylike(values) -> ArrayLike:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_array_like is True for Series and Index so the return type should probably be AnyArrayLike

"""
ensure that we are arraylike if not already
"""
Expand Down Expand Up @@ -323,7 +323,7 @@ def get_data_algo(values: ArrayLike):
return htable, values


def _check_object_for_strings(values) -> str:
def _check_object_for_strings(values: np.ndarray) -> str:
"""
Check if we can use string hashtable instead of object hashtable.

Expand Down Expand Up @@ -527,7 +527,11 @@ def f(c, v):


def factorize_array(
values: np.ndarray, na_sentinel: int = -1, size_hint=None, na_value=None, mask=None
values: np.ndarray,
na_sentinel: int = -1,
size_hint: Optional[int] = None,
na_value=None,
mask: Optional[np.ndarray] = None,
) -> Tuple[np.ndarray, np.ndarray]:
"""
Factorize an array-like to codes and uniques.
Expand Down Expand Up @@ -982,13 +986,13 @@ def mode(values, dropna: bool = True) -> Series:


def rank(
values,
values: ArrayLike,
axis: int = 0,
method: str = "average",
na_option: str = "keep",
ascending: bool = True,
pct: bool = False,
):
) -> np.ndarray:
"""
Rank the values along a given axis.

Expand Down Expand Up @@ -1038,7 +1042,12 @@ def rank(
return ranks


def checked_add_with_arr(arr, b, arr_mask=None, b_mask=None):
def checked_add_with_arr(
arr: np.ndarray,
b,
arr_mask: Optional[np.ndarray] = None,
b_mask: Optional[np.ndarray] = None,
) -> np.ndarray:
"""
Perform array addition that checks for underflow and overflow.

Expand All @@ -1051,9 +1060,9 @@ def checked_add_with_arr(arr, b, arr_mask=None, b_mask=None):
----------
arr : array addend.
b : array or scalar addend.
arr_mask : boolean array or None
arr_mask : np.ndarray[bool] or None, default None
array indicating which elements to exclude from checking
b_mask : boolean array or boolean or None
b_mask : np.ndarray[bool] or None, default None
array or scalar indicating which element(s) to exclude from checking

Returns
Expand Down Expand Up @@ -1406,7 +1415,9 @@ def get_indexer(current_indexer, other_indexer):


def _view_wrapper(f, arr_dtype=None, out_dtype=None, fill_wrap=None):
def wrapper(arr, indexer, out, fill_value=np.nan):
def wrapper(
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
):
if arr_dtype is not None:
arr = arr.view(arr_dtype)
if out_dtype is not None:
Expand All @@ -1419,7 +1430,9 @@ def wrapper(arr, indexer, out, fill_value=np.nan):


def _convert_wrapper(f, conv_dtype):
def wrapper(arr, indexer, out, fill_value=np.nan):
def wrapper(
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value=np.nan
):
if conv_dtype == object:
# GH#39755 avoid casting dt64/td64 to integers
arr = ensure_wrapped_if_datetimelike(arr)
Expand All @@ -1429,7 +1442,9 @@ def wrapper(arr, indexer, out, fill_value=np.nan):
return wrapper


def _take_2d_multi_object(arr, indexer, out, fill_value, mask_info):
def _take_2d_multi_object(
arr: np.ndarray, indexer: np.ndarray, out: np.ndarray, fill_value, mask_info
) -> None:
# this is not ideal, performance-wise, but it's better than raising
# an exception (best to optimize in Cython to avoid getting here)
row_idx, col_idx = indexer
Expand All @@ -1452,7 +1467,14 @@ def _take_2d_multi_object(arr, indexer, out, fill_value, mask_info):
out[i, j] = arr[u_, v]


def _take_nd_object(arr, indexer, out, axis: int, fill_value, mask_info):
def _take_nd_object(
arr: np.ndarray,
indexer: np.ndarray,
out: np.ndarray,
axis: int,
fill_value,
mask_info,
):
if mask_info is not None:
mask, needs_masking = mask_info
else:
Expand Down Expand Up @@ -1570,7 +1592,7 @@ def _take_nd_object(arr, indexer, out, axis: int, fill_value, mask_info):


def _get_take_nd_function(
ndim: int, arr_dtype, out_dtype, axis: int = 0, mask_info=None
ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: int = 0, mask_info=None
):
if ndim <= 2:
tup = (arr_dtype.name, out_dtype.name)
Expand Down Expand Up @@ -1605,7 +1627,9 @@ def func2(arr, indexer, out, fill_value=np.nan):
return func2


def take(arr, indices, axis: int = 0, allow_fill: bool = False, fill_value=None):
def take(
arr, indices: np.ndarray, axis: int = 0, allow_fill: bool = False, fill_value=None
):
"""
Take elements from an array.

Expand Down Expand Up @@ -1739,7 +1763,7 @@ def take_nd(
arr,
indexer,
axis: int = 0,
out=None,
out: Optional[np.ndarray] = None,
fill_value=lib.no_default,
allow_fill: bool = True,
):
Expand Down
Loading