diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index d1f8957859337..832d09b062265 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -4,6 +4,7 @@ from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError +from pandas.util._decorators import cache_readonly from pandas.core.algorithms import take, unique from pandas.core.arrays.base import ExtensionArray @@ -64,6 +65,8 @@ def _validate_fill_value(self, fill_value): # ------------------------------------------------------------------------ + # TODO: make this a cache_readonly; for that to work we need to remove + # the _index_data kludge in libreduction @property def shape(self) -> Tuple[int, ...]: return self._ndarray.shape @@ -71,15 +74,15 @@ def shape(self) -> Tuple[int, ...]: def __len__(self) -> int: return self.shape[0] - @property + @cache_readonly def ndim(self) -> int: return len(self.shape) - @property + @cache_readonly def size(self) -> int: return np.prod(self.shape) - @property + @cache_readonly def nbytes(self) -> int: return self._ndarray.nbytes diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index e07e2da164cac..257a51b423308 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -454,6 +454,8 @@ class DatetimeLikeArrayMixin( # ------------------------------------------------------------------ # NDArrayBackedExtensionArray compat + # TODO: make this a cache_readonly; need to get around _index_data + # kludge in libreduction @property def _ndarray(self) -> np.ndarray: # NB: A bunch of Interval tests fail if we use ._data @@ -526,6 +528,13 @@ def __getitem__(self, key): only handle list-likes, slices, and integer scalars """ + if lib.is_integer(key): + # fast-path + result = self._data[key] + if self.ndim == 1: + return self._box_func(result) + return self._simple_new(result, dtype=self.dtype) + if com.is_bool_indexer(key): # first convert to boolean, because check_array_indexer doesn't # allow object dtype