Skip to content

Commit b8da43f

Browse files
jbrockmendelJulianWgs
authored andcommitted
CLN/PERF: remove unused out kwd in take_nd (pandas-dev#40510)
1 parent c03d930 commit b8da43f

File tree

8 files changed

+48
-224
lines changed

8 files changed

+48
-224
lines changed

pandas/core/array_algos/take.py

+35-53
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import functools
44
from typing import (
55
TYPE_CHECKING,
6-
Optional,
76
overload,
87
)
98

@@ -33,7 +32,6 @@ def take_nd(
3332
arr: np.ndarray,
3433
indexer,
3534
axis: int = ...,
36-
out: Optional[np.ndarray] = ...,
3735
fill_value=...,
3836
allow_fill: bool = ...,
3937
) -> np.ndarray:
@@ -45,7 +43,6 @@ def take_nd(
4543
arr: ExtensionArray,
4644
indexer,
4745
axis: int = ...,
48-
out: Optional[np.ndarray] = ...,
4946
fill_value=...,
5047
allow_fill: bool = ...,
5148
) -> ArrayLike:
@@ -56,7 +53,6 @@ def take_nd(
5653
arr: ArrayLike,
5754
indexer,
5855
axis: int = 0,
59-
out: Optional[np.ndarray] = None,
6056
fill_value=lib.no_default,
6157
allow_fill: bool = True,
6258
) -> ArrayLike:
@@ -79,10 +75,6 @@ def take_nd(
7975
indices are filed with fill_value
8076
axis : int, default 0
8177
Axis to take from
82-
out : ndarray or None, default None
83-
Optional output array, must be appropriate type to hold input and
84-
fill_value together, if indexer has any -1 value entries; call
85-
maybe_promote to determine this type for any fill_value
8678
fill_value : any, default np.nan
8779
Fill value to replace -1 values with
8880
allow_fill : boolean, default True
@@ -104,14 +96,13 @@ def take_nd(
10496
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
10597

10698
arr = np.asarray(arr)
107-
return _take_nd_ndarray(arr, indexer, axis, out, fill_value, allow_fill)
99+
return _take_nd_ndarray(arr, indexer, axis, fill_value, allow_fill)
108100

109101

110102
def _take_nd_ndarray(
111103
arr: np.ndarray,
112104
indexer,
113105
axis: int,
114-
out: Optional[np.ndarray],
115106
fill_value,
116107
allow_fill: bool,
117108
) -> np.ndarray:
@@ -121,8 +112,12 @@ def _take_nd_ndarray(
121112
dtype, fill_value = arr.dtype, arr.dtype.type()
122113
else:
123114
indexer = ensure_platform_int(indexer)
124-
indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
125-
arr, indexer, out, fill_value, allow_fill
115+
116+
if not allow_fill:
117+
return arr.take(indexer, axis=axis)
118+
119+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
120+
arr, indexer, fill_value
126121
)
127122

128123
flip_order = False
@@ -132,23 +127,20 @@ def _take_nd_ndarray(
132127
if flip_order:
133128
arr = arr.T
134129
axis = arr.ndim - axis - 1
135-
if out is not None:
136-
out = out.T
137130

138131
# at this point, it's guaranteed that dtype can hold both the arr values
139132
# and the fill_value
140-
if out is None:
141-
out_shape_ = list(arr.shape)
142-
out_shape_[axis] = len(indexer)
143-
out_shape = tuple(out_shape_)
144-
if arr.flags.f_contiguous and axis == arr.ndim - 1:
145-
# minor tweak that can make an order-of-magnitude difference
146-
# for dataframes initialized directly from 2-d ndarrays
147-
# (s.t. df.values is c-contiguous and df._mgr.blocks[0] is its
148-
# f-contiguous transpose)
149-
out = np.empty(out_shape, dtype=dtype, order="F")
150-
else:
151-
out = np.empty(out_shape, dtype=dtype)
133+
out_shape_ = list(arr.shape)
134+
out_shape_[axis] = len(indexer)
135+
out_shape = tuple(out_shape_)
136+
if arr.flags.f_contiguous and axis == arr.ndim - 1:
137+
# minor tweak that can make an order-of-magnitude difference
138+
# for dataframes initialized directly from 2-d ndarrays
139+
# (s.t. df.values is c-contiguous and df._mgr.blocks[0] is its
140+
# f-contiguous transpose)
141+
out = np.empty(out_shape, dtype=dtype, order="F")
142+
else:
143+
out = np.empty(out_shape, dtype=dtype)
152144

153145
func = _get_take_nd_function(
154146
arr.ndim, arr.dtype, out.dtype, axis=axis, mask_info=mask_info
@@ -192,8 +184,8 @@ def take_1d(
192184
if not allow_fill:
193185
return arr.take(indexer)
194186

195-
indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
196-
arr, indexer, None, fill_value, allow_fill
187+
dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
188+
arr, indexer, fill_value
197189
)
198190

199191
# at this point, it's guaranteed that dtype can hold both the arr values
@@ -517,32 +509,22 @@ def _take_2d_multi_object(
517509
def _take_preprocess_indexer_and_fill_value(
518510
arr: np.ndarray,
519511
indexer: np.ndarray,
520-
out: Optional[np.ndarray],
521512
fill_value,
522-
allow_fill: bool,
523513
):
524514
mask_info = None
525515

526-
if not allow_fill:
527-
dtype, fill_value = arr.dtype, arr.dtype.type()
528-
mask_info = None, False
529-
else:
530-
# check for promotion based on types only (do this first because
531-
# it's faster than computing a mask)
532-
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
533-
if dtype != arr.dtype and (out is None or out.dtype != dtype):
534-
# check if promotion is actually required based on indexer
535-
mask = indexer == -1
536-
needs_masking = mask.any()
537-
mask_info = mask, needs_masking
538-
if needs_masking:
539-
if out is not None and out.dtype != dtype:
540-
raise TypeError("Incompatible type for fill_value")
541-
else:
542-
# if not, then depromote, set fill_value to dummy
543-
# (it won't be used but we don't want the cython code
544-
# to crash when trying to cast it to dtype)
545-
dtype, fill_value = arr.dtype, arr.dtype.type()
546-
547-
indexer = ensure_platform_int(indexer)
548-
return indexer, dtype, fill_value, mask_info
516+
# check for promotion based on types only (do this first because
517+
# it's faster than computing a mask)
518+
dtype, fill_value = maybe_promote(arr.dtype, fill_value)
519+
if dtype != arr.dtype:
520+
# check if promotion is actually required based on indexer
521+
mask = indexer == -1
522+
needs_masking = mask.any()
523+
mask_info = mask, needs_masking
524+
if not needs_masking:
525+
# if not, then depromote, set fill_value to dummy
526+
# (it won't be used but we don't want the cython code
527+
# to crash when trying to cast it to dtype)
528+
dtype, fill_value = arr.dtype, arr.dtype.type()
529+
530+
return dtype, fill_value, mask_info

pandas/core/groupby/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ def _transform_with_numba(self, data, func, *args, engine_kwargs=None, **kwargs)
11601160
group_keys = self.grouper._get_group_keys()
11611161
labels, _, n_groups = self.grouper.group_info
11621162
sorted_index = get_group_index_sorter(labels, n_groups)
1163-
sorted_labels = algorithms.take_nd(labels, sorted_index, allow_fill=False)
1163+
sorted_labels = labels.take(sorted_index)
11641164
sorted_data = data.take(sorted_index, axis=self.axis).to_numpy()
11651165
starts, ends = lib.generate_slices(sorted_labels, n_groups)
11661166

@@ -1195,7 +1195,7 @@ def _aggregate_with_numba(self, data, func, *args, engine_kwargs=None, **kwargs)
11951195
group_keys = self.grouper._get_group_keys()
11961196
labels, _, n_groups = self.grouper.group_info
11971197
sorted_index = get_group_index_sorter(labels, n_groups)
1198-
sorted_labels = algorithms.take_nd(labels, sorted_index, allow_fill=False)
1198+
sorted_labels = labels.take(sorted_index)
11991199
sorted_data = data.take(sorted_index, axis=self.axis).to_numpy()
12001200
starts, ends = lib.generate_slices(sorted_labels, n_groups)
12011201

pandas/core/groupby/ops.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
maybe_fill,
7272
)
7373

74-
import pandas.core.algorithms as algorithms
7574
from pandas.core.base import SelectionMixin
7675
import pandas.core.common as com
7776
from pandas.core.frame import DataFrame
@@ -756,7 +755,7 @@ def _aggregate_series_fast(self, obj: Series, func: F):
756755
# avoids object / Series creation overhead
757756
indexer = get_group_index_sorter(group_index, ngroups)
758757
obj = obj.take(indexer)
759-
group_index = algorithms.take_nd(group_index, indexer, allow_fill=False)
758+
group_index = group_index.take(indexer)
760759
grouper = libreduction.SeriesGrouper(obj, func, group_index, ngroups)
761760
result, counts = grouper.get_result()
762761
return result, counts
@@ -989,9 +988,9 @@ def __init__(self, data: FrameOrSeries, labels, ngroups: int, axis: int = 0):
989988
assert isinstance(axis, int), axis
990989

991990
@cache_readonly
992-
def slabels(self):
991+
def slabels(self) -> np.ndarray:
993992
# Sorted labels
994-
return algorithms.take_nd(self.labels, self._sort_idx, allow_fill=False)
993+
return self.labels.take(self._sort_idx)
995994

996995
@cache_readonly
997996
def _sort_idx(self) -> np.ndarray: # np.ndarray[np.intp]

pandas/core/indexes/base.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,7 @@ def _union(self, other: Index, sort):
29802980
missing = algos.unique1d(self.get_indexer_non_unique(other)[1])
29812981

29822982
if len(missing) > 0:
2983-
other_diff = algos.take_nd(rvals, missing, allow_fill=False)
2983+
other_diff = rvals.take(missing)
29842984
result = concat_compat((lvals, other_diff))
29852985
else:
29862986
# error: Incompatible types in assignment (expression has type
@@ -4254,9 +4254,7 @@ def _get_leaf_sorter(labels: List[np.ndarray]) -> np.ndarray:
42544254
)
42554255

42564256
if right_lev_indexer is not None:
4257-
right_indexer = algos.take_nd(
4258-
right_lev_indexer, join_index.codes[level], allow_fill=False
4259-
)
4257+
right_indexer = right_lev_indexer.take(join_index.codes[level])
42604258
else:
42614259
right_indexer = join_index.codes[level]
42624260

pandas/core/indexes/multi.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -3532,14 +3532,10 @@ def equals(self, other: object) -> bool:
35323532
if not np.array_equal(self_mask, other_mask):
35333533
return False
35343534
self_codes = self_codes[~self_mask]
3535-
self_values = algos.take_nd(
3536-
np.asarray(self.levels[i]._values), self_codes, allow_fill=False
3537-
)
3535+
self_values = self.levels[i]._values.take(self_codes)
35383536

35393537
other_codes = other_codes[~other_mask]
3540-
other_values = algos.take_nd(
3541-
np.asarray(other.levels[i]._values), other_codes, allow_fill=False
3542-
)
3538+
other_values = other.levels[i]._values.take(other_codes)
35433539

35443540
# since we use NaT both datetime64 and timedelta64 we can have a
35453541
# situation where a level is typed say timedelta64 in self (IOW it

pandas/core/internals/managers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def items(self) -> Index:
306306

307307
def get_dtypes(self):
308308
dtypes = np.array([blk.dtype for blk in self.blocks])
309-
return algos.take_nd(dtypes, self.blknos, allow_fill=False)
309+
return dtypes.take(self.blknos)
310310

311311
@property
312312
def arrays(self) -> List[ArrayLike]:
@@ -1177,7 +1177,7 @@ def value_getitem(placement):
11771177
is_deleted = np.zeros(self.nblocks, dtype=np.bool_)
11781178
is_deleted[removed_blknos] = True
11791179

1180-
new_blknos = np.empty(self.nblocks, dtype=np.int64)
1180+
new_blknos = np.empty(self.nblocks, dtype=np.intp)
11811181
new_blknos.fill(-1)
11821182
new_blknos[~is_deleted] = np.arange(self.nblocks - len(removed_blknos))
11831183
self._blknos = new_blknos[self._blknos]

pandas/core/sorting.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from pandas.core.dtypes.generic import ABCMultiIndex
3434
from pandas.core.dtypes.missing import isna
3535

36-
import pandas.core.algorithms as algorithms
3736
from pandas.core.construction import extract_array
3837

3938
if TYPE_CHECKING:
@@ -643,10 +642,10 @@ def _reorder_by_uniques(uniques, labels):
643642
mask = labels < 0
644643

645644
# move labels to right locations (ie, unsort ascending labels)
646-
labels = algorithms.take_nd(reverse_indexer, labels, allow_fill=False)
645+
labels = reverse_indexer.take(labels)
647646
np.putmask(labels, mask, -1)
648647

649648
# sort observed ids
650-
uniques = algorithms.take_nd(uniques, sorter, allow_fill=False)
649+
uniques = uniques.take(sorter)
651650

652651
return uniques, labels

0 commit comments

Comments
 (0)