Skip to content

Commit 8c674dd

Browse files
jbrockmendelrhshadrach
authored andcommitted
CLN: simplify DTA.__getitem__ (pandas-dev#33714)
1 parent 2a547f3 commit 8c674dd

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

pandas/core/arrays/datetimelike.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -538,21 +538,6 @@ def __getitem__(self, key):
538538
This getitem defers to the underlying array, which by-definition can
539539
only handle list-likes, slices, and integer scalars
540540
"""
541-
is_int = lib.is_integer(key)
542-
if lib.is_scalar(key) and not is_int:
543-
raise IndexError(
544-
"only integers, slices (`:`), ellipsis (`...`), "
545-
"numpy.newaxis (`None`) and integer or boolean "
546-
"arrays are valid indices"
547-
)
548-
549-
getitem = self._data.__getitem__
550-
if is_int:
551-
val = getitem(key)
552-
if lib.is_scalar(val):
553-
# i.e. self.ndim == 1
554-
return self._box_func(val)
555-
return type(self)(val, dtype=self.dtype)
556541

557542
if com.is_bool_indexer(key):
558543
# first convert to boolean, because check_array_indexer doesn't
@@ -569,6 +554,16 @@ def __getitem__(self, key):
569554
else:
570555
key = check_array_indexer(self, key)
571556

557+
freq = self._get_getitem_freq(key)
558+
result = self._data[key]
559+
if lib.is_scalar(result):
560+
return self._box_func(result)
561+
return self._simple_new(result, dtype=self.dtype, freq=freq)
562+
563+
def _get_getitem_freq(self, key):
564+
"""
565+
Find the `freq` attribute to assign to the result of a __getitem__ lookup.
566+
"""
572567
is_period = is_period_dtype(self.dtype)
573568
if is_period:
574569
freq = self.freq
@@ -583,11 +578,7 @@ def __getitem__(self, key):
583578
# GH#21282 indexing with Ellipsis is similar to a full slice,
584579
# should preserve `freq` attribute
585580
freq = self.freq
586-
587-
result = getitem(key)
588-
if lib.is_scalar(result):
589-
return self._box_func(result)
590-
return self._simple_new(result, dtype=self.dtype, freq=freq)
581+
return freq
591582

592583
def __setitem__(
593584
self,

0 commit comments

Comments
 (0)