Skip to content

PERF: Datetimelike lookups #33933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,22 +65,24 @@ 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

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

Expand Down
9 changes: 9 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down