Skip to content

TYP: misc #43977

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 2 commits into from
Oct 14, 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
216 changes: 137 additions & 79 deletions pandas/_libs/algos.pyi

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions pandas/_libs/hashing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ cdef inline uint64_t _rotl(uint64_t x, uint64_t b) nogil:
return (x << b) | (x >> (64 - b))


cdef inline void u32to8_le(uint8_t* p, uint32_t v) nogil:
p[0] = <uint8_t>(v)
p[1] = <uint8_t>(v >> 8)
p[2] = <uint8_t>(v >> 16)
p[3] = <uint8_t>(v >> 24)


cdef inline uint64_t u8to64_le(uint8_t* p) nogil:
return (<uint64_t>p[0] |
<uint64_t>p[1] << 8 |
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ cdef class StringHashTable(HashTable):
not None, then _additionally_ any value "val" satisfying
val == na_value is considered missing.
mask : ndarray[bool], optional
Not yet implementd for StringHashTable.
Not yet implemented for StringHashTable.

Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/index.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class BaseMultiIndexCodesEngine:
values: np.ndarray, # np.ndarray[object] of tuples
method: str,
limit: int | None,
) -> np.ndarray: ... # np.ndarray[np.int64]
) -> npt.NDArray[np.intp]: ...
31 changes: 19 additions & 12 deletions pandas/core/array_algos/take.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
algos as libalgos,
lib,
)
from pandas._typing import ArrayLike
from pandas._typing import (
ArrayLike,
npt,
)

from pandas.core.dtypes.cast import maybe_promote
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -110,7 +113,7 @@ def take_nd(

def _take_nd_ndarray(
arr: np.ndarray,
indexer,
indexer: npt.NDArray[np.intp] | None,
axis: int,
fill_value,
allow_fill: bool,
Expand All @@ -122,7 +125,7 @@ def _take_nd_ndarray(
else:
indexer = ensure_platform_int(indexer)

indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
arr, indexer, fill_value, allow_fill
)

Expand Down Expand Up @@ -160,30 +163,32 @@ def _take_nd_ndarray(

def take_1d(
arr: ArrayLike,
indexer: np.ndarray,
indexer: npt.NDArray[np.intp],
fill_value=None,
allow_fill: bool = True,
) -> ArrayLike:
"""
Specialized version for 1D arrays. Differences compared to `take_nd`:

- Assumes input array has already been converted to numpy array / EA
- Assumes indexer is already guaranteed to be int64 dtype ndarray
- Assumes indexer is already guaranteed to be intp dtype ndarray
- Only works for 1D arrays

To ensure the lowest possible overhead.

Note: similarly to `take_nd`, this function assumes that the indexer is
a valid(ated) indexer with no out of bound indices.
"""
indexer = ensure_platform_int(indexer)

if not isinstance(arr, np.ndarray):
# ExtensionArray -> dispatch to their method
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)

if not allow_fill:
return arr.take(indexer)

indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
arr, indexer, fill_value, True
)

Expand All @@ -200,7 +205,9 @@ def take_1d(


def take_2d_multi(
arr: np.ndarray, indexer: tuple[np.ndarray, np.ndarray], fill_value=np.nan
arr: np.ndarray,
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
fill_value=np.nan,
) -> np.ndarray:
"""
Specialized Cython take which sets NaN values in one pass.
Expand Down Expand Up @@ -249,6 +256,7 @@ def take_2d_multi(
if func is not None:
func(arr, indexer, out=out, fill_value=fill_value)
else:
# test_reindex_multi
_take_2d_multi_object(
arr, indexer, out, fill_value=fill_value, mask_info=mask_info
)
Expand Down Expand Up @@ -457,7 +465,7 @@ def wrapper(

def _take_nd_object(
arr: np.ndarray,
indexer: np.ndarray, # np.ndarray[np.intp]
indexer: npt.NDArray[np.intp],
out: np.ndarray,
axis: int,
fill_value,
Expand All @@ -480,7 +488,7 @@ def _take_nd_object(

def _take_2d_multi_object(
arr: np.ndarray,
indexer: tuple[np.ndarray, np.ndarray],
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
out: np.ndarray,
fill_value,
mask_info,
Expand Down Expand Up @@ -509,7 +517,7 @@ def _take_2d_multi_object(

def _take_preprocess_indexer_and_fill_value(
arr: np.ndarray,
indexer: np.ndarray,
indexer: npt.NDArray[np.intp],
fill_value,
allow_fill: bool,
):
Expand All @@ -533,5 +541,4 @@ def _take_preprocess_indexer_and_fill_value(
# to crash when trying to cast it to dtype)
dtype, fill_value = arr.dtype, arr.dtype.type()

indexer = ensure_platform_int(indexer)
return indexer, dtype, fill_value, mask_info
return dtype, fill_value, mask_info
12 changes: 9 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4672,17 +4672,23 @@ def _reindex_columns(
allow_dups=False,
)

def _reindex_multi(self, axes, copy: bool, fill_value) -> DataFrame:
def _reindex_multi(
self, axes: dict[str, Index], copy: bool, fill_value
) -> DataFrame:
"""
We are guaranteed non-Nones in the axes.
"""

new_index, row_indexer = self.index.reindex(axes["index"])
new_columns, col_indexer = self.columns.reindex(axes["columns"])

if row_indexer is not None and col_indexer is not None:
# Fastpath. By doing two 'take's at once we avoid making an
# unnecessary copy.
# We only get here with `not self._is_mixed_type`, which (almost)
# ensures that self.values is cheap. It may be worth making this
# condition more specific.
indexer = row_indexer, col_indexer
# error: Argument 2 to "take_2d_multi" has incompatible type "Tuple[Any,
# Any]"; expected "ndarray"
new_values = take_2d_multi(self.values, indexer, fill_value=fill_value)
return self._constructor(new_values, index=new_index, columns=new_columns)
else:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
_T = TypeVar("_T", bound="NDArrayBackedExtensionIndex")


def inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
def _inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
"""
Make an alias for a method of the underlying ExtensionArray.

Expand Down Expand Up @@ -130,7 +130,7 @@ def inherit_names(names: list[str], delegate, cache: bool = False, wrap: bool =

def wrapper(cls):
for name in names:
meth = inherit_from_data(name, delegate, cache=cache, wrap=wrap)
meth = _inherit_from_data(name, delegate, cache=cache, wrap=wrap)
setattr(cls, name, meth)

return cls
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def test_apply_series_on_date_time_index_aware_series(dti, exp, aware):
tm.assert_frame_equal(result, exp)


def test_apply_scaler_on_date_time_index_aware_series():
def test_apply_scalar_on_date_time_index_aware_series():
# GH 25959
# Calling apply on a localized time series should not cause an error
series = tm.makeTimeSeries(nper=30).tz_localize("UTC")
Expand Down