Skip to content

Commit 8f65498

Browse files
committed
changes
1 parent 21d39b5 commit 8f65498

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

pandas/core/indexes/range.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class RangeIndex(Int64Index):
6767
_engine_type = libindex.Int64Engine
6868

6969
# check whether self._data has benn called
70-
_has_called_data = False # type: bool
70+
_cached_data = None # type: np.ndarray
7171
# --------------------------------------------------------------------
7272
# Constructors
7373

@@ -186,10 +186,12 @@ def _constructor(self):
186186
""" return the class to use for construction """
187187
return Int64Index
188188

189-
@cache_readonly
190-
def _data(self):
191-
self._has_called_data = True
192-
return np.arange(self._start, self._stop, self._step, dtype=np.int64)
189+
@property
190+
def _data(self) -> np.ndarray:
191+
if self._cached_data is None:
192+
self._cached_data = np.arange(self._start, self._stop, self._step,
193+
dtype=np.int64)
194+
return self._cached_data
193195

194196
@cache_readonly
195197
def _int64index(self):
@@ -223,7 +225,7 @@ def _format_data(self, name=None):
223225
return None
224226

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

228230
# --------------------------------------------------------------------
229231
@property

pandas/tests/indexes/test_range.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -241,35 +241,40 @@ def test_view(self):
241241
def test_dtype(self):
242242
assert self.index.dtype == np.int64
243243

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

250251
repr(idx)
251-
assert idx._has_called_data is False
252+
assert idx._cached_data is None
252253

253254
str(idx)
254-
assert idx._has_called_data is False
255+
assert idx._cached_data is None
255256

256257
idx.get_loc(20)
257-
assert idx._has_called_data is False
258+
assert idx._cached_data is None
258259

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

261262
df.loc[50]
262-
assert idx._has_called_data is False
263+
assert idx._cached_data is None
263264

264265
with pytest.raises(KeyError):
265266
df.loc[51]
266-
assert idx._has_called_data is False
267+
assert idx._cached_data is None
267268

268269
df.loc[10:50]
269-
assert idx._has_called_data is False
270+
assert idx._cached_data is None
270271

271272
df.iloc[5:10]
272-
assert idx._has_called_data is False
273+
assert idx._cached_data is None
274+
275+
# actually calling data._data
276+
assert isinstance(idx._data, np.ndarray)
277+
assert isinstance(idx._cached_data, np.ndarray)
273278

274279
def test_is_monotonic(self):
275280
assert self.index.is_monotonic is True

0 commit comments

Comments
 (0)