Skip to content

Commit 17cdcd9

Browse files
authored
PERF: Index.take to check for full range indices (#56806)
* Index.take to check is_range_indexer * whatsnew * MultiIndex.take to check is_range_indexer * ensure 1-dim
1 parent f87680d commit 17cdcd9

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

doc/source/whatsnew/v2.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Deprecations
101101

102102
Performance improvements
103103
~~~~~~~~~~~~~~~~~~~~~~~~
104-
-
104+
- Performance improvement in :meth:`Index.take` when ``indices`` is a full range indexer from zero to length of index (:issue:`56806`)
105105
-
106106

107107
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,9 @@ def take(
11571157
indices = ensure_platform_int(indices)
11581158
allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices)
11591159

1160+
if indices.ndim == 1 and lib.is_range_indexer(indices, len(self)):
1161+
return self.copy()
1162+
11601163
# Note: we discard fill_value and use self._na_value, only relevant
11611164
# in the case where allow_fill is True and fill_value is not None
11621165
values = self._values

pandas/core/indexes/multi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,9 @@ def take(
22472247
# only fill if we are passing a non-None fill_value
22482248
allow_fill = self._maybe_disallow_fill(allow_fill, fill_value, indices)
22492249

2250+
if indices.ndim == 1 and lib.is_range_indexer(indices, len(self)):
2251+
return self.copy()
2252+
22502253
na_value = -1
22512254

22522255
taken = [lab.take(indices) for lab in self.codes]

0 commit comments

Comments
 (0)