Skip to content

PERF: make RangeIndex iterate over ._range #35676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions asv_bench/benchmarks/index_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def time_datetime_difference_disjoint(self):

class Range:
def setup(self):
self.idx_inc = RangeIndex(start=0, stop=10 ** 7, step=3)
self.idx_dec = RangeIndex(start=10 ** 7, stop=-1, step=-3)
self.idx_inc = RangeIndex(start=0, stop=10 ** 6, step=3)
self.idx_dec = RangeIndex(start=10 ** 6, stop=-1, step=-3)

def time_max(self):
self.idx_inc.max()
Expand All @@ -73,15 +73,23 @@ def time_min_trivial(self):
self.idx_inc.min()

def time_get_loc_inc(self):
self.idx_inc.get_loc(900000)
self.idx_inc.get_loc(900_000)

def time_get_loc_dec(self):
self.idx_dec.get_loc(100000)
self.idx_dec.get_loc(100_000)

def time_iter_inc(self):
for _ in self.idx_inc:
pass

def time_iter_dec(self):
for _ in self.idx_dec:
pass


class IndexEquals:
def setup(self):
idx_large_fast = RangeIndex(100000)
idx_large_fast = RangeIndex(100_000)
idx_small_slow = date_range(start="1/1/2012", periods=1)
self.mi_large_slow = MultiIndex.from_product([idx_large_fast, idx_small_slow])

Expand All @@ -94,7 +102,7 @@ def time_non_object_equals_multiindex(self):
class IndexAppend:
def setup(self):

N = 10000
N = 10_000
self.range_idx = RangeIndex(0, 100)
self.int_idx = self.range_idx.astype(int)
self.obj_idx = self.int_idx.astype(str)
Expand Down Expand Up @@ -168,7 +176,7 @@ def time_get_loc_non_unique_sorted(self, dtype):
class Float64IndexMethod:
# GH 13166
def setup(self):
N = 100000
N = 100_000
a = np.arange(N)
self.ind = Float64Index(a * 4.8000000418824129e-08)

Expand Down Expand Up @@ -212,7 +220,7 @@ class GC:
params = [1, 2, 5]

def create_use_drop(self):
idx = Index(list(range(1000 * 1000)))
idx = Index(list(range(1_000_000)))
idx._engine

def peakmem_gc_instances(self, N):
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
def tolist(self):
return list(self._range)

@doc(Int64Index.__iter__)
def __iter__(self):
yield from self._range

@doc(Int64Index._shallow_copy)
def _shallow_copy(self, values=None, name: Label = no_default):
name = self.name if name is no_default else name
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def test_cache(self):
idx.any()
assert idx._cache == {}

for _ in idx:
pass
assert idx._cache == {}

df = pd.DataFrame({"a": range(10)}, index=idx)

df.loc[50]
Expand Down