8000 PERF: Do not init cache in RangeIndex.take by GianlucaFicarelli · Pull Request #53397 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

PERF: Do not init cache in RangeIndex.take #53397

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 9 commits into from
Jul 18, 2023
Prev Previous commit
Next Next commit
Move checks from test_cache_after_calling_loc_with_array to test_cache
  • Loading branch information
GianlucaFicarelli committed Jul 14, 2023
commit 637d1684fe289f133ff7d38fa0ceb7f7a76e7acb
28 changes: 10 additions & 18 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_dtype(self, simple_index):
assert index.dtype == np.int64

def test_cache(self):
# GH 26565, GH26617, GH35432
# GH 26565, GH26617, GH35432, GH53387
# This test checks whether _cache has been set.
# Calling RangeIndex._cache["_data"] creates an int64 array of the same length
# as the RangeIndex and stores it in _cache.
Expand Down Expand Up @@ -264,23 +264,7 @@ def test_cache(self):
df.iloc[5:10]
assert idx._cache == {}

# idx._cache should contain a _data entry after call to idx._data
idx._data
assert isinstance(idx._data, np.ndarray)
assert idx._data is idx._data # check cached value is reused
assert len(idx._cache) == 1
expected = np.arange(0, 100, 10, dtype="int64")
tm.assert_numpy_array_equal(idx._cache["_data"], expected)

def test_cache_after_calling_loc_with_array(self):
# GH 53387
# the cache will contain a _constructor key, so it should be tested separately
idx = RangeIndex(0, 100, 10)
df = pd.DataFrame({"a": range(10)}, index=idx)

assert "_data" not in idx._cache

# take is internally called by loc, but it's also tested explicitly
# after calling take, _cache may contain other keys, but not "_data"
idx.take([3, 0, 1])
assert "_data" not in idx._cache

Expand All @@ -290,6 +274,14 @@ def test_cache_after_calling_loc_with_array(self):
df.iloc[[5, 6, 7, 8, 9]]
assert "_data" not in idx._cache

# idx._cache should contain a _data entry after call to idx._data
idx._data
assert isinstance(idx._data, np.ndarray)
assert idx._data is idx._data # check cached value is reused
assert "_data" in idx._cache
expected = np.arange(0, 100, 10, dtype="int64")
tm.assert_numpy_array_equal(idx._cache["_data"], expected)

def test_is_monotonic(self):
index = RangeIndex(0, 20, 2)
assert index.is_monotonic_increasing is True
Expand Down
0