Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0432bf2

Browse files
authoredOct 14, 2021
TYP: Misc in Algos(#43977)
1 parent 14f9184 commit 0432bf2

File tree

8 files changed

+170
-106
lines changed

8 files changed

+170
-106
lines changed
 

‎pandas/_libs/algos.pyi

Lines changed: 137 additions & 79 deletions
Large diffs are not rendered by default.

‎pandas/_libs/hashing.pyx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,6 @@ cdef inline uint64_t _rotl(uint64_t x, uint64_t b) nogil:
120120
return (x << b) | (x >> (64 - b))
121121

122122

123-
cdef inline void u32to8_le(uint8_t* p, uint32_t v) nogil:
124-
p[0] = <uint8_t>(v)
125-
p[1] = <uint8_t>(v >> 8)
126-
p[2] = <uint8_t>(v >> 16)
127-
p[3] = <uint8_t>(v >> 24)
128-
129-
130123
cdef inline uint64_t u8to64_le(uint8_t* p) nogil:
131124
return (<uint64_t>p[0] |
132125
<uint64_t>p[1] << 8 |

‎pandas/_libs/hashtable_class_helper.pxi.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ cdef class StringHashTable(HashTable):
10461046
not None, then _additionally_ any value "val" satisfying
10471047
val == na_value is considered missing.
10481048
mask : ndarray[bool], optional
1049-
Not yet implementd for StringHashTable.
1049+
Not yet implemented for StringHashTable.
10501050

10511051
Returns
10521052
-------

‎pandas/_libs/index.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ class BaseMultiIndexCodesEngine:
6262
values: np.ndarray, # np.ndarray[object] of tuples
6363
method: str,
6464
limit: int | None,
65-
) -> np.ndarray: ... # np.ndarray[np.int64]
65+
) -> npt.NDArray[np.intp]: ...

‎pandas/core/array_algos/take.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
algos as libalgos,
1414
lib,
1515
)
16-
from pandas._typing import ArrayLike
16+
from pandas._typing import (
17+
ArrayLike,
18+
npt,
19+
)
1720

1821
from pandas.core.dtypes.cast import maybe_promote
1922
from pandas.core.dtypes.common import (
@@ -110,7 +113,7 @@ def take_nd(
110113

111114
def _take_nd_ndarray(
112115
arr: np.ndarray,
113-
indexer,
116+
indexer: npt.NDArray[np.intp] | None,
114117
axis: int,
115118
fill_value,
116119
allow_fill: bool,
@@ -122,7 +125,7 @@ def _take_nd_ndarray(
122125
else:
123126
indexer = ensure_platform_int(indexer)
124127

125-
indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
128+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
126129
arr, indexer, fill_value, allow_fill
127130
)
128131

@@ -160,30 +163,32 @@ def _take_nd_ndarray(
160163

161164
def take_1d(
162165
arr: ArrayLike,
163-
indexer: np.ndarray,
166+
indexer: npt.NDArray[np.intp],
164167
fill_value=None,
165168
allow_fill: bool = True,
166169
) -> ArrayLike:
167170
"""
168171
Specialized version for 1D arrays. Differences compared to `take_nd`:
169172
170173
- Assumes input array has already been converted to numpy array / EA
171-
- Assumes indexer is already guaranteed to be int64 dtype ndarray
174+
- Assumes indexer is already guaranteed to be intp dtype ndarray
172175
- Only works for 1D arrays
173176
174177
To ensure the lowest possible overhead.
175178
176179
Note: similarly to `take_nd`, this function assumes that the indexer is
177180
a valid(ated) indexer with no out of bound indices.
178181
"""
182+
indexer = ensure_platform_int(indexer)
183+
179184
if not isinstance(arr, np.ndarray):
180185
# ExtensionArray -> dispatch to their method
181186
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
182187

183188
if not allow_fill:
184189
return arr.take(indexer)
185190

186-
indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
191+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
187192
arr, indexer, fill_value, True
188193
)
189194

@@ -200,7 +205,9 @@ def take_1d(
200205

201206

202207
def take_2d_multi(
203-
arr: np.ndarray, indexer: tuple[np.ndarray, np.ndarray], fill_value=np.nan
208+
arr: np.ndarray,
209+
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
210+
fill_value=np.nan,
204211
) -> np.ndarray:
205212
"""
206213
Specialized Cython take which sets NaN values in one pass.
@@ -249,6 +256,7 @@ def take_2d_multi(
249256
if func is not None:
250257
func(arr, indexer, out=out, fill_value=fill_value)
251258
else:
259+
# test_reindex_multi
252260
_take_2d_multi_object(
253261
arr, indexer, out, fill_value=fill_value, mask_info=mask_info
254262
)
@@ -457,7 +465,7 @@ def wrapper(
457465

458466
def _take_nd_object(
459467
arr: np.ndarray,
460-
indexer: np.ndarray, # np.ndarray[np.intp]
468+
indexer: npt.NDArray[np.intp],
461469
out: np.ndarray,
462470
axis: int,
463471
fill_value,
@@ -480,7 +488,7 @@ def _take_nd_object(
480488

481489
def _take_2d_multi_object(
482490
arr: np.ndarray,
483-
indexer: tuple[np.ndarray, np.ndarray],
491+
indexer: tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]],
484492
out: np.ndarray,
485493
fill_value,
486494
mask_info,
@@ -509,7 +517,7 @@ def _take_2d_multi_object(
509517

510518
def _take_preprocess_indexer_and_fill_value(
511519
arr: np.ndarray,
512-
indexer: np.ndarray,
520+
indexer: npt.NDArray[np.intp],
513521
fill_value,
514522
allow_fill: bool,
515523
):
@@ -533,5 +541,4 @@ def _take_preprocess_indexer_and_fill_value(
533541
# to crash when trying to cast it to dtype)
534542
dtype, fill_value = arr.dtype, arr.dtype.type()
535543

536-
indexer = ensure_platform_int(indexer)
537-
return indexer, dtype, fill_value, mask_info
544+
return dtype, fill_value, mask_info

‎pandas/core/frame.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4672,17 +4672,23 @@ def _reindex_columns(
46724672
allow_dups=False,
46734673
)
46744674

4675-
def _reindex_multi(self, axes, copy: bool, fill_value) -> DataFrame:
4675+
def _reindex_multi(
4676+
self, axes: dict[str, Index], copy: bool, fill_value
4677+
) -> DataFrame:
46764678
"""
46774679
We are guaranteed non-Nones in the axes.
46784680
"""
4681+
46794682
new_index, row_indexer = self.index.reindex(axes["index"])
46804683
new_columns, col_indexer = self.columns.reindex(axes["columns"])
46814684

46824685
if row_indexer is not None and col_indexer is not None:
4686+
# Fastpath. By doing two 'take's at once we avoid making an
4687+
# unnecessary copy.
4688+
# We only get here with `not self._is_mixed_type`, which (almost)
4689+
# ensures that self.values is cheap. It may be worth making this
4690+
# condition more specific.
46834691
indexer = row_indexer, col_indexer
4684-
# error: Argument 2 to "take_2d_multi" has incompatible type "Tuple[Any,
4685-
# Any]"; expected "ndarray"
46864692
new_values = take_2d_multi(self.values, indexer, fill_value=fill_value)
46874693
return self._constructor(new_values, index=new_index, columns=new_columns)
46884694
else:

‎pandas/core/indexes/extension.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
_T = TypeVar("_T", bound="NDArrayBackedExtensionIndex")
3232

3333

34-
def inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
34+
def _inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
3535
"""
3636
Make an alias for a method of the underlying ExtensionArray.
3737
@@ -120,7 +120,7 @@ def inherit_names(names: list[str], delegate, cache: bool = False, wrap: bool =
120120

121121
def wrapper(cls):
122122
for name in names:
123-
meth = inherit_from_data(name, delegate, cache=cache, wrap=wrap)
123+
meth = _inherit_from_data(name, delegate, cache=cache, wrap=wrap)
124124
setattr(cls, name, meth)
125125

126126
return cls

‎pandas/tests/apply/test_series_apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def test_apply_series_on_date_time_index_aware_series(dti, exp, aware):
774774
tm.assert_frame_equal(result, exp)
775775

776776

777-
def test_apply_scaler_on_date_time_index_aware_series():
777+
def test_apply_scalar_on_date_time_index_aware_series():
778778
# GH 25959
779779
# Calling apply on a localized time series should not cause an error
780780
series = tm.makeTimeSeries(nper=30).tz_localize("UTC")

0 commit comments

Comments
 (0)
Please sign in to comment.