Skip to content

Commit 7b2e975

Browse files
committed
cache and remove boxing
1 parent 02906ce commit 7b2e975

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

asv_bench/benchmarks/period.py

+25
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,28 @@ def time_value_counts_pindex(self):
4949
self.i.value_counts()
5050

5151

52+
class period_standard_indexing(object):
53+
goal_time = 0.2
54+
55+
def setup(self):
56+
self.index = PeriodIndex(start='1985', periods=1000, freq='D')
57+
self.series = Series(range(1000), index=self.index)
58+
self.period = self.index[500]
59+
60+
def time_get_loc(self):
61+
self.index.get_loc(self.period)
62+
63+
def time_shape(self):
64+
self.index.shape
65+
66+
def time_shallow_copy(self):
67+
self.index._shallow_copy()
68+
69+
def time_series_loc(self):
70+
self.series.loc[self.period]
71+
72+
def time_align(self):
73+
pd.DataFrame({'a': self.series, 'b': self.series[:500]})
74+
75+
def time_intersection(self):
76+
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
.. _whatsnew_0192.enhancements.other:

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
@@ -65,6 +65,7 @@ def dt64arr_to_periodarr(data, freq, tz):
6565

6666
# --- Period index sketch
6767

68+
6869
_DIFFERENT_FREQ_INDEX = period._DIFFERENT_FREQ_INDEX
6970

7071

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

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

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

331334
def _coerce_scalar_to_index(self, item):
@@ -356,9 +359,8 @@ def __contains__(self, key):
356359
def asi8(self):
357360
return self._values.view('i8')
358361

359-
@property
362+
@cache_readonly
360363
def _int64index(self):
361-
# do not cache, same as .asi8
362364
return Int64Index(self.asi8, name=self.name, fastpath=True)
363365

364366
@property

0 commit comments

Comments
 (0)