Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed May 31, 2019
1 parent 21d39b5 commit ff805da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
12 changes: 7 additions & 5 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class RangeIndex(Int64Index):
_engine_type = libindex.Int64Engine

# check whether self._data has benn called
_has_called_data = False # type: bool
_cached_data = None # type: np.ndarray
# --------------------------------------------------------------------
# Constructors

Expand Down Expand Up @@ -186,10 +186,12 @@ def _constructor(self):
""" return the class to use for construction """
return Int64Index

@cache_readonly
@property
def _data(self):
self._has_called_data = True
return np.arange(self._start, self._stop, self._step, dtype=np.int64)
if self._cached_data is None:
self._cached_data = np.arange(self._start, self._stop, self._step,
dtype=np.int64)
return self._cached_data

@cache_readonly
def _int64index(self):
Expand Down Expand Up @@ -223,7 +225,7 @@ def _format_data(self, name=None):
return None

def _format_with_header(self, header, na_rep='NaN', **kwargs):
return header + [pprint_thing(x) for x in self._range]
return header + list(map(pprint_thing, self._range))

# --------------------------------------------------------------------
@property
Expand Down
27 changes: 16 additions & 11 deletions pandas/tests/indexes/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,35 +241,40 @@ def test_view(self):
def test_dtype(self):
assert self.index.dtype == np.int64

def test_has_called_data(self):
# Calling RangeIndex._data caches a array of the same length.
# This tests whether RangeIndex._data has been called by doing methods
def test_cached_data(self):
# Calling RangeIndex._data caches an int64 array of the same length at
# self._cached_data. This tests whether _cached_data has been set.
idx = RangeIndex(0, 100, 10)
assert idx._has_called_data is False

assert idx._cached_data is None

repr(idx)
assert idx._has_called_data is False
assert idx._cached_data is None

str(idx)
assert idx._has_called_data is False
assert idx._cached_data is None

idx.get_loc(20)
assert idx._has_called_data is False
assert idx._cached_data is None

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

df.loc[50]
assert idx._has_called_data is False
assert idx._cached_data is None

with pytest.raises(KeyError):
df.loc[51]
assert idx._has_called_data is False
assert idx._cached_data is None

df.loc[10:50]
assert idx._has_called_data is False
assert idx._cached_data is None

df.iloc[5:10]
assert idx._has_called_data is False
assert idx._cached_data is None

# actually calling data._data
assert isinstance(idx._data, np.ndarray)
assert isinstance(idx._cached_data, np.ndarray)

def test_is_monotonic(self):
assert self.index.is_monotonic is True
Expand Down

0 comments on commit ff805da

Please sign in to comment.