Skip to content

CLN: Minor cleanup of caching #40225

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 2 commits into from
Mar 5, 2021
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
5 changes: 3 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class PandasObject(DirNamesMixin):
Baseclass for various pandas objects.
"""

# results from calls to methods decorated with cache_readonly get added to _cache
_cache: Dict[str, Any]

@property
Expand All @@ -100,14 +101,14 @@ def _reset_cache(self, key: Optional[str] = None) -> None:
"""
Reset cached properties. If ``key`` is passed, only clears that key.
"""
if getattr(self, "_cache", None) is None:
if not hasattr(self, "_cache"):
return
if key is None:
self._cache.clear()
else:
self._cache.pop(key, None)

def __sizeof__(self):
def __sizeof__(self) -> int:
"""
Generates the total memory usage for an object that returns
either a value or Series of values
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def _format_with_header(self, header: List[str], na_rep: str = "NaN") -> List[st
"instead"
)

@cache_readonly
@property
def start(self):
"""
The value of the `start` parameter (``0`` if this was not supplied).
Expand All @@ -261,7 +261,7 @@ def _start(self):
)
return self.start

@cache_readonly
@property
def stop(self):
"""
The value of the `stop` parameter.
Expand All @@ -284,7 +284,7 @@ def _stop(self):
)
return self.stop

@cache_readonly
@property
def step(self):
"""
The value of the `step` parameter (``1`` if this was not supplied).
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_cache(self):
idx._data
assert isinstance(idx._data, np.ndarray)
assert idx._data is idx._data # check cached value is reused
assert len(idx._cache) == 4
assert len(idx._cache) == 1
expected = np.arange(0, 100, 10, dtype="int64")
tm.assert_numpy_array_equal(idx._cache["_data"], expected)

Expand Down