@@ -631,34 +631,35 @@ def __hash__(self):
631
631
raise TypeError ("unhashable type: %r" % type (self ).__name__ )
632
632
633
633
def __getitem__ (self , key ):
634
- """Override numpy.ndarray's __getitem__ method to work as desired"""
635
- arr_idx = self .view (np .ndarray )
634
+ """
635
+ Override numpy.ndarray's __getitem__ method to work as desired.
636
+
637
+ This function adds lists and Series as valid boolean indexers
638
+ (ndarrays only supports ndarray with dtype=bool).
639
+
640
+ If resulting ndim != 1, plain ndarray is returned instead of
641
+ corresponding `Index` subclass.
642
+
643
+ """
644
+ # There's no custom logic to be implemented in __getslice__, so it's
645
+ # not overloaded intentionally.
646
+ __getitem__ = super (Index , self ).__getitem__
636
647
if np .isscalar (key ):
637
- return arr_idx [key ]
638
- else :
639
- if com ._is_bool_indexer (key ):
640
- key = np .asarray (key )
648
+ return __getitem__ (key )
641
649
642
- try :
643
- result = arr_idx [key ]
644
- if result .ndim > 1 :
645
- return result
646
- except (IndexError ):
647
- if not len (key ):
648
- result = []
649
- else :
650
- raise
650
+ if isinstance (key , slice ):
651
+ # This case is separated from the conditional above to avoid
652
+ # pessimization of basic indexing.
653
+ return __getitem__ (key )
651
654
652
- return Index (result , name = self .name )
655
+ if com ._is_bool_indexer (key ):
656
+ return __getitem__ (np .asarray (key ))
653
657
654
- def _getitem_slice (self , key ):
655
- """ getitem for a bool/sliceable, fallback to standard getitem """
656
- try :
657
- arr_idx = self .view (np .ndarray )
658
- result = arr_idx [key ]
659
- return self .__class__ (result , name = self .name , fastpath = True )
660
- except :
661
- return self .__getitem__ (key )
658
+ result = __getitem__ (key )
659
+ if result .ndim > 1 :
660
+ return result .view (np .ndarray )
661
+ else :
662
+ return result
662
663
663
664
def append (self , other ):
664
665
"""
@@ -2800,8 +2801,6 @@ def __getitem__(self, key):
2800
2801
2801
2802
return result
2802
2803
2803
- _getitem_slice = __getitem__
2804
-
2805
2804
def take (self , indexer , axis = None ):
2806
2805
"""
2807
2806
Analogous to ndarray.take
0 commit comments