|
|
@@ -631,34 +631,35 @@ def __hash__(self): |
|
|
raise TypeError("unhashable type: %r" % type(self).__name__)
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
- """Override numpy.ndarray's __getitem__ method to work as desired"""
|
|
|
- arr_idx = self.view(np.ndarray)
|
|
|
+ """
|
|
|
+ Override numpy.ndarray's __getitem__ method to work as desired.
|
|
|
+
|
|
|
+ This function adds lists and Series as valid boolean indexers
|
|
|
+ (ndarrays only supports ndarray with dtype=bool).
|
|
|
+
|
|
|
+ If resulting ndim != 1, plain ndarray is returned instead of
|
|
|
+ corresponding `Index` subclass.
|
|
|
+
|
|
|
+ """
|
|
|
+ # There's no custom logic to be implemented in __getslice__, so it's
|
|
|
+ # not overloaded intentionally.
|
|
|
+ __getitem__ = super(Index, self).__getitem__
|
|
|
if np.isscalar(key):
|
|
|
- return arr_idx[key]
|
|
|
- else:
|
|
|
- if com._is_bool_indexer(key):
|
|
|
- key = np.asarray(key)
|
|
|
+ return __getitem__(key)
|
|
|
|
|
|
- try:
|
|
|
- result = arr_idx[key]
|
|
|
- if result.ndim > 1:
|
|
|
- return result
|
|
|
- except (IndexError):
|
|
|
- if not len(key):
|
|
|
- result = []
|
|
|
- else:
|
|
|
- raise
|
|
|
+ if isinstance(key, slice):
|
|
|
+ # This case is separated from the conditional above to avoid
|
|
|
+ # pessimization of basic indexing.
|
|
|
+ return __getitem__(key)
|
|
|
|
|
|
- return Index(result, name=self.name)
|
|
|
+ if com._is_bool_indexer(key):
|
|
|
+ return __getitem__(np.asarray(key))
|
|
|
|
|
|
- def _getitem_slice(self, key):
|
|
|
- """ getitem for a bool/sliceable, fallback to standard getitem """
|
|
|
- try:
|
|
|
- arr_idx = self.view(np.ndarray)
|
|
|
- result = arr_idx[key]
|
|
|
- return self.__class__(result, name=self.name, fastpath=True)
|
|
|
- except:
|
|
|
- return self.__getitem__(key)
|
|
|
+ result = __getitem__(key)
|
|
|
+ if result.ndim > 1:
|
|
|
+ return result.view(np.ndarray)
|
|
|
+ else:
|
|
|
+ return result
|
|
|
|
|
|
def append(self, other):
|
|
|
"""
|
|
|
@@ -2800,8 +2801,6 @@ def __getitem__(self, key): |
|
|
|
|
|
return result
|
|
|
|
|
|
- _getitem_slice = __getitem__
|
|
|
-
|
|
|
def take(self, indexer, axis=None):
|
|
|
"""
|
|
|
Analogous to ndarray.take
|
|
|
|