Skip to content

Commit 0bb677e

Browse files
authored
PERF: Datetimelike lookups (#33933)
1 parent f76c4c2 commit 0bb677e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pandas/core/arrays/_mixins.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from pandas.compat.numpy import function as nv
66
from pandas.errors import AbstractMethodError
7+
from pandas.util._decorators import cache_readonly
78

89
from pandas.core.algorithms import take, unique
910
from pandas.core.arrays.base import ExtensionArray
@@ -64,22 +65,24 @@ def _validate_fill_value(self, fill_value):
6465

6566
# ------------------------------------------------------------------------
6667

68+
# TODO: make this a cache_readonly; for that to work we need to remove
69+
# the _index_data kludge in libreduction
6770
@property
6871
def shape(self) -> Tuple[int, ...]:
6972
return self._ndarray.shape
7073

7174
def __len__(self) -> int:
7275
return self.shape[0]
7376

74-
@property
77+
@cache_readonly
7578
def ndim(self) -> int:
7679
return len(self.shape)
7780

78-
@property
81+
@cache_readonly
7982
def size(self) -> int:
8083
return np.prod(self.shape)
8184

82-
@property
85+
@cache_readonly
8386
def nbytes(self) -> int:
8487
return self._ndarray.nbytes
8588

pandas/core/arrays/datetimelike.py

+9
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ class DatetimeLikeArrayMixin(
454454
# ------------------------------------------------------------------
455455
# NDArrayBackedExtensionArray compat
456456

457+
# TODO: make this a cache_readonly; need to get around _index_data
458+
# kludge in libreduction
457459
@property
458460
def _ndarray(self) -> np.ndarray:
459461
# NB: A bunch of Interval tests fail if we use ._data
@@ -526,6 +528,13 @@ def __getitem__(self, key):
526528
only handle list-likes, slices, and integer scalars
527529
"""
528530

531+
if lib.is_integer(key):
532+
# fast-path
533+
result = self._data[key]
534+
if self.ndim == 1:
535+
return self._box_func(result)
536+
return self._simple_new(result, dtype=self.dtype)
537+
529538
if com.is_bool_indexer(key):
530539
# first convert to boolean, because check_array_indexer doesn't
531540
# allow object dtype

0 commit comments

Comments
 (0)