Skip to content
10 changes: 4 additions & 6 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,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 @@ -115,6 +114,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 @@ -191,10 +191,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 @@ -213,6 +210,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 @@ -231,7 +231,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 @@ -291,7 +291,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 @@ -495,7 +495,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 @@ -950,13 +954,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 @@ -1006,7 +1010,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 @@ -1019,9 +1028,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 @@ -1374,7 +1383,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 @@ -1387,7 +1398,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 @@ -1397,7 +1410,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 @@ -1420,7 +1435,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 @@ -1538,7 +1560,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 @@ -1573,7 +1595,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 @@ -1707,7 +1731,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