Skip to content

Commit 09a622b

Browse files
committed
CLN: Collapse private ._take implementation into the public take method pandas-dev#27174
1 parent 5a7a8e1 commit 09a622b

File tree

7 files changed

+33
-65
lines changed

7 files changed

+33
-65
lines changed

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2937,7 +2937,7 @@ def _ixs(self, i, axis=0):
29372937
return self.loc[:, lab_slice]
29382938
else:
29392939
if isinstance(label, Index):
2940-
return self._take(i, axis=1)
2940+
return self.take(i, axis=1)
29412941

29422942
index_len = len(self.index)
29432943

@@ -2999,7 +2999,7 @@ def __getitem__(self, key):
29992999
if getattr(indexer, "dtype", None) == bool:
30003000
indexer = np.where(indexer)[0]
30013001

3002-
data = self._take(indexer, axis=1)
3002+
data = self.take(indexer, axis=1)
30033003

30043004
if is_single_key:
30053005
# What does looking for a single key in a non-unique index return?
@@ -3032,7 +3032,7 @@ def _getitem_bool_array(self, key):
30323032
# be reindexed to match DataFrame rows
30333033
key = check_bool_indexer(self.index, key)
30343034
indexer = key.nonzero()[0]
3035-
return self._take(indexer, axis=0)
3035+
return self.take(indexer, axis=0)
30363036

30373037
def _getitem_multilevel(self, key):
30383038
loc = self.columns.get_loc(key)

pandas/core/generic.py

+19-52
Original file line numberDiff line numberDiff line change
@@ -3294,7 +3294,7 @@ def _iget_item_cache(self, item):
32943294
if ax.is_unique:
32953295
lower = self._get_item_cache(ax[item])
32963296
else:
3297-
lower = self._take(item, axis=self._info_axis_number)
3297+
lower = self.take(item, axis=self._info_axis_number)
32983298
return lower
32993299

33003300
def _box_item_values(self, key, values):
@@ -3522,52 +3522,6 @@ def __delitem__(self, key):
35223522
except KeyError:
35233523
pass
35243524

3525-
def _take(self, indices, axis=0, is_copy=True):
3526-
"""
3527-
Return the elements in the given *positional* indices along an axis.
3528-
3529-
This means that we are not indexing according to actual values in
3530-
the index attribute of the object. We are indexing according to the
3531-
actual position of the element in the object.
3532-
3533-
This is the internal version of ``.take()`` and will contain a wider
3534-
selection of parameters useful for internal use but not as suitable
3535-
for public usage.
3536-
3537-
Parameters
3538-
----------
3539-
indices : array-like
3540-
An array of ints indicating which positions to take.
3541-
axis : int, default 0
3542-
The axis on which to select elements. "0" means that we are
3543-
selecting rows, "1" means that we are selecting columns, etc.
3544-
is_copy : bool, default True
3545-
Whether to return a copy of the original object or not.
3546-
3547-
Returns
3548-
-------
3549-
taken : same type as caller
3550-
An array-like containing the elements taken from the object.
3551-
3552-
See Also
3553-
--------
3554-
numpy.ndarray.take
3555-
numpy.take
3556-
"""
3557-
self._consolidate_inplace()
3558-
3559-
new_data = self._data.take(
3560-
indices, axis=self._get_block_manager_axis(axis), verify=True
3561-
)
3562-
result = self._constructor(new_data).__finalize__(self)
3563-
3564-
# Maybe set copy if we didn't actually change the index.
3565-
if is_copy:
3566-
if not result._get_axis(axis).equals(self._get_axis(axis)):
3567-
result._set_is_copy(self)
3568-
3569-
return result
3570-
35713525
def take(self, indices, axis=0, is_copy=True, **kwargs):
35723526
"""
35733527
Return the elements in the given *positional* indices along an axis.
@@ -3644,7 +3598,20 @@ class max_speed
36443598
3 lion mammal 80.5
36453599
"""
36463600
nv.validate_take(tuple(), kwargs)
3647-
return self._take(indices, axis=axis, is_copy=is_copy)
3601+
3602+
self._consolidate_inplace()
3603+
3604+
new_data = self._data.take(
3605+
indices, axis=self._get_block_manager_axis(axis), verify=True
3606+
)
3607+
result = self._constructor(new_data).__finalize__(self)
3608+
3609+
# Maybe set copy if we didn't actually change the index.
3610+
if is_copy:
3611+
if not result._get_axis(axis).equals(self._get_axis(axis)):
3612+
result._set_is_copy(self)
3613+
3614+
return result
36483615

36493616
def xs(self, key, axis=0, level=None, drop_level=True):
36503617
"""
@@ -3773,9 +3740,9 @@ class animal locomotion
37733740
if isinstance(loc, np.ndarray):
37743741
if loc.dtype == np.bool_:
37753742
inds, = loc.nonzero()
3776-
return self._take(inds, axis=axis)
3743+
return self.take(inds, axis=axis)
37773744
else:
3778-
return self._take(loc, axis=axis)
3745+
return self.take(loc, axis=axis)
37793746

37803747
if not is_scalar(loc):
37813748
new_index = self.index[loc]
@@ -8091,7 +8058,7 @@ def at_time(self, time, asof=False, axis=None):
80918058
except AttributeError:
80928059
raise TypeError("Index must be DatetimeIndex")
80938060

8094-
return self._take(indexer, axis=axis)
8061+
return self.take(indexer, axis=axis)
80958062

80968063
def between_time(
80978064
self, start_time, end_time, include_start=True, include_end=True, axis=None
@@ -8168,7 +8135,7 @@ def between_time(
81688135
except AttributeError:
81698136
raise TypeError("Index must be DatetimeIndex")
81708137

8171-
return self._take(indexer, axis=axis)
8138+
return self.take(indexer, axis=axis)
81728139

81738140
def resample(
81748141
self,

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def get_group(self, name, obj=None):
679679
if not len(inds):
680680
raise KeyError(name)
681681

682-
return obj._take(inds, axis=self.axis)
682+
return obj.take(inds, axis=self.axis)
683683

684684
def __iter__(self):
685685
"""

pandas/core/groupby/grouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _set_grouper(self, obj, sort=False):
194194
# use stable sort to support first, last, nth
195195
indexer = self.indexer = ax.argsort(kind="mergesort")
196196
ax = ax.take(indexer)
197-
obj = obj._take(indexer, axis=self.axis, is_copy=False)
197+
obj = obj.take(indexer, axis=self.axis, is_copy=False)
198198

199199
self.obj = obj
200200
self.grouper = ax

pandas/core/groupby/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def _aggregate_series_fast(self, obj, func):
675675
# avoids object / Series creation overhead
676676
dummy = obj._get_values(slice(None, 0))
677677
indexer = get_group_index_sorter(group_index, ngroups)
678-
obj = obj._take(indexer)
678+
obj = obj.take(indexer)
679679
group_index = algorithms.take_nd(group_index, indexer, allow_fill=False)
680680
grouper = reduction.SeriesGrouper(obj, func, group_index, ngroups, dummy)
681681
result, counts = grouper.get_result()
@@ -915,7 +915,7 @@ def __iter__(self):
915915
yield i, self._chop(sdata, slice(start, end))
916916

917917
def _get_sorted_data(self):
918-
return self.data._take(self.sort_idx, axis=self.axis)
918+
return self.data.take(self.sort_idx, axis=self.axis)
919919

920920
def _chop(self, sdata, slice_obj):
921921
return sdata.iloc[slice_obj]

pandas/core/indexing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ def _getitem_iterable(self, key, axis: int):
11371137
# A boolean indexer
11381138
key = check_bool_indexer(labels, key)
11391139
inds, = key.nonzero()
1140-
return self.obj._take(inds, axis=axis)
1140+
return self.obj.take(inds, axis=axis)
11411141
else:
11421142
# A collection of keys
11431143
keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
@@ -1448,7 +1448,7 @@ def _getbool_axis(self, key, axis: int):
14481448
key = check_bool_indexer(labels, key)
14491449
inds, = key.nonzero()
14501450
try:
1451-
return self.obj._take(inds, axis=axis)
1451+
return self.obj.take(inds, axis=axis)
14521452
except Exception as detail:
14531453
raise self._exception(detail)
14541454

@@ -1469,7 +1469,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int):
14691469
else:
14701470
# DatetimeIndex overrides Index.slice_indexer and may
14711471
# return a DatetimeIndex instead of a slice object.
1472-
return self.obj._take(indexer, axis=axis)
1472+
return self.obj.take(indexer, axis=axis)
14731473

14741474

14751475
class _LocIndexer(_LocationIndexer):
@@ -2138,7 +2138,7 @@ def _get_list_axis(self, key, axis: int):
21382138
Series object
21392139
"""
21402140
try:
2141-
return self.obj._take(key, axis=axis)
2141+
return self.obj.take(key, axis=axis)
21422142
except IndexError:
21432143
# re-raise with different error message
21442144
raise IndexError("positional indexers are out-of-bounds")

pandas/core/series.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4371,8 +4371,9 @@ def memory_usage(self, index=True, deep=False):
43714371
v += self.index.memory_usage(deep=deep)
43724372
return v
43734373

4374-
@Appender(generic.NDFrame._take.__doc__)
4375-
def _take(self, indices, axis=0, is_copy=False):
4374+
@Appender(generic.NDFrame.take.__doc__)
4375+
def take(self, indices, axis=0, is_copy=False, **kwargs):
4376+
nv.validate_take(tuple(), kwargs)
43764377

43774378
indices = ensure_platform_int(indices)
43784379
new_index = self.index.take(indices)

0 commit comments

Comments
 (0)