Skip to content

Commit a8d8fae

Browse files
MaximilianRjorisvandenbossche
authored andcommitted
cache and remove boxing (pandas-dev#14931)
(cherry picked from commit 4c3d4d4)
1 parent 85bc6d7 commit a8d8fae

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

asv_bench/benchmarks/period.py

+25
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,28 @@ def time_period_index_value_counts(self):
4747
self.i.value_counts()
4848

4949

50+
class period_standard_indexing(object):
51+
goal_time = 0.2
52+
53+
def setup(self):
54+
self.index = PeriodIndex(start='1985', periods=1000, freq='D')
55+
self.series = Series(range(1000), index=self.index)
56+
self.period = self.index[500]
57+
58+
def time_get_loc(self):
59+
self.index.get_loc(self.period)
60+
61+
def time_shape(self):
62+
self.index.shape
63+
64+
def time_shallow_copy(self):
65+
self.index._shallow_copy()
66+
67+
def time_series_loc(self):
68+
self.series.loc[self.period]
69+
70+
def time_align(self):
71+
pd.DataFrame({'a': self.series, 'b': self.series[:500]})
72+
73+
def time_intersection(self):
74+
self.index[:750].intersection(self.index[250:])

doc/source/whatsnew/v0.19.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Performance Improvements
2222
~~~~~~~~~~~~~~~~~~~~~~~~
2323

2424
- Improved performance of ``.replace()`` (:issue:`12745`)
25+
- Improved performance of ``PeriodIndex`` (:issue:`14822`)
2526
- Improved performance ``Series`` creation with a datetime index and dictionary data (:issue:`14894`)
2627

2728

pandas/core/base.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def transpose(self, *args, **kwargs):
814814
@property
815815
def shape(self):
816816
""" return a tuple of the shape of the underlying data """
817-
return self.values.shape
817+
return self._values.shape
818818

819819
@property
820820
def ndim(self):
@@ -842,22 +842,22 @@ def data(self):
842842
@property
843843
def itemsize(self):
844844
""" return the size of the dtype of the item of the underlying data """
845-
return self.values.itemsize
845+
return self._values.itemsize
846846

847847
@property
848848
def nbytes(self):
849849
""" return the number of bytes in the underlying data """
850-
return self.values.nbytes
850+
return self._values.nbytes
851851

852852
@property
853853
def strides(self):
854854
""" return the strides of the underlying data """
855-
return self.values.strides
855+
return self._values.strides
856856

857857
@property
858858
def size(self):
859859
""" return the number of elements in the underlying data """
860-
return self.values.size
860+
return self._values.size
861861

862862
@property
863863
def flags(self):

pandas/tseries/period.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def dt64arr_to_periodarr(data, freq, tz):
6464

6565
# --- Period index sketch
6666

67+
6768
_DIFFERENT_FREQ_INDEX = period._DIFFERENT_FREQ_INDEX
6869

6970

@@ -304,7 +305,7 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
304305
if (len(values) > 0 and is_float_dtype(values)):
305306
raise TypeError("PeriodIndex can't take floats")
306307
else:
307-
return PeriodIndex(values, name=name, freq=freq, **kwargs)
308+
return cls(values, name=name, freq=freq, **kwargs)
308309

309310
values = np.array(values, dtype='int64', copy=False)
310311

@@ -325,6 +326,8 @@ def _shallow_copy(self, values=None, **kwargs):
325326
if kwargs.get('freq') is None:
326327
# freq must be provided
327328
kwargs['freq'] = self.freq
329+
if values is None:
330+
values = self._values
328331
return super(PeriodIndex, self)._shallow_copy(values=values, **kwargs)
329332

330333
def _coerce_scalar_to_index(self, item):
@@ -355,9 +358,8 @@ def __contains__(self, key):
355358
def asi8(self):
356359
return self._values.view('i8')
357360

358-
@property
361+
@cache_readonly
359362
def _int64index(self):
360-
# do not cache, same as .asi8
361363
return Int64Index(self.asi8, name=self.name, fastpath=True)
362364

363365
@property

pandas/tseries/tests/test_period.py

-8
Original file line numberDiff line numberDiff line change
@@ -2101,14 +2101,6 @@ def test_comp_period(self):
21012101
exp = idx.values < idx.values[10]
21022102
self.assert_numpy_array_equal(result, exp)
21032103

2104-
def test_getitem_ndim2(self):
2105-
idx = period_range('2007-01', periods=3, freq='M')
2106-
2107-
result = idx[:, None]
2108-
# MPL kludge, internally has incorrect shape
2109-
tm.assertIsInstance(result, PeriodIndex)
2110-
self.assertEqual(result.shape, (len(idx), ))
2111-
21122104
def test_getitem_index(self):
21132105
idx = period_range('2007-01', periods=10, freq='M', name='x')
21142106

0 commit comments

Comments
 (0)