Skip to content

Commit 3b0b781

Browse files
committed
PERF: do not instantiate IndexEngine for standard lookup over RangeIndex
closes #16685
1 parent 2175f3e commit 3b0b781

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ Performance improvements
639639
int8/int16/int32 and the searched key is within the integer bounds for the dtype (:issue:`22034`)
640640
- Improved performance of :meth:`pandas.core.groupby.GroupBy.quantile` (:issue:`20405`)
641641
- Improved performance of slicing and other selected operation on a :class:`RangeIndex` (:issue:`26565`, :issue:`26617`, :issue:`26722`)
642+
- :class:`RangeIndex` now performs standard lookup without instantiating an actual hashtable, hence saving memory (:issue:`16685`)
642643
- Improved performance of :meth:`read_csv` by faster tokenizing and faster parsing of small float numbers (:issue:`25784`)
643644
- Improved performance of :meth:`read_csv` by faster parsing of N/A and boolean values (:issue:`25804`)
644645
- Improved performance of :attr:`IntervalIndex.is_monotonic`, :attr:`IntervalIndex.is_monotonic_increasing` and :attr:`IntervalIndex.is_monotonic_decreasing` by removing conversion to :class:`MultiIndex` (:issue:`24813`)

pandas/core/indexes/range.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
from pandas.core.dtypes import concat as _concat
1515
from pandas.core.dtypes.common import (
16-
ensure_python_int, is_int64_dtype, is_integer, is_scalar,
17-
is_timedelta64_dtype)
16+
ensure_python_int, is_int64_dtype, is_integer, is_scalar, is_integer_dtype,
17+
is_timedelta64_dtype, is_list_like, ensure_platform_int)
1818
from pandas.core.dtypes.generic import (
1919
ABCDataFrame, ABCSeries, ABCTimedeltaIndex)
2020

@@ -348,6 +348,36 @@ def get_loc(self, key, method=None, tolerance=None):
348348
raise KeyError(key)
349349
return super().get_loc(key, method=method, tolerance=tolerance)
350350

351+
@Appender(_index_shared_docs['get_indexer'])
352+
def get_indexer(self, target, method=None, limit=None, tolerance=None):
353+
if not (method is None and tolerance is None and is_list_like(target)):
354+
return super().get_indexer(target, method=method,
355+
tolerance=tolerance)
356+
357+
if self.step > 0:
358+
start, stop, step = self.start, self.stop, self.step
359+
else:
360+
# Work on reversed range for simplicity:
361+
start, stop, step = (self.stop - self.step,
362+
self.start + 1,
363+
- self.step)
364+
365+
target_array = np.asarray(target)
366+
if not (is_integer_dtype(target_array) and target_array.ndim == 1):
367+
# checks/conversions/roundings are delegated to general method
368+
return super().get_indexer(target, method=method,
369+
tolerance=tolerance)
370+
371+
locs = target_array - start
372+
valid = (locs % step == 0) & (locs >= 0) & (target_array < stop)
373+
locs[~valid] = -1
374+
locs[valid] = locs[valid] / step
375+
376+
if step != self.step:
377+
# We reversed this range: transform to original locs
378+
locs[valid] = len(self) - 1 - locs[valid]
379+
return ensure_platform_int(locs)
380+
351381
def tolist(self):
352382
return list(self._range)
353383

pandas/tests/indexes/test_range.py

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
from pandas import Float64Index, Index, Int64Index, RangeIndex, Series
88
import pandas.util.testing as tm
9+
from pandas.core.dtypes.common import ensure_platform_int
910

1011
from .test_numeric import Numeric
1112

@@ -965,3 +966,23 @@ def test_append(self, appends):
965966
# Append single item rather than list
966967
result2 = indices[0].append(indices[1])
967968
tm.assert_index_equal(result2, expected, exact=True)
969+
970+
def test_engineless_lookup(self):
971+
# GH 16685
972+
# Standard lookup on RangeIndex should not require the engine to be
973+
# created
974+
idx = RangeIndex(2, 10, 3)
975+
976+
assert idx.get_loc(5) == 1
977+
tm.assert_numpy_array_equal(idx.get_indexer([2, 8]),
978+
ensure_platform_int(np.array([0, 2])))
979+
with pytest.raises(KeyError):
980+
idx.get_loc(3)
981+
982+
assert '_engine' not in idx._cache
983+
984+
# The engine is still required for lookup of a different dtype scalar:
985+
with pytest.raises(KeyError):
986+
assert idx.get_loc('a') == -1
987+
988+
assert '_engine' in idx._cache

0 commit comments

Comments
 (0)