def _getitem_multilevel(self, key): loc = self.columns.get_loc(key) if isinstance(loc, (slice, Series, np.ndarray, Index)): new_columns = self.columns[loc] result_columns = maybe_droplevels(new_columns, key) if self._is_mixed_type: result = self.reindex(columns=new_columns) result.columns = result_columns else: new_values = self.values[:, loc] result = self._constructor(new_values, index=self.index, columns=result_columns) result = result.__finalize__(self) if len(result.columns) == 1: top = result.columns[0] if isinstance(top, tuple): top = top[0] if top == '': result = result[''] if isinstance(result, Series): result = self._constructor_sliced(result, index=self.index, name=key) result._set_is_copy(self) return result else: return self._get_item_cache(key)
def _getitem_multilevel(self, key): info = self._info_axis loc = info.get_loc(key) if isinstance(loc, (slice, np.ndarray)): new_index = info[loc] result_index = maybe_droplevels(new_index, key) slices = [loc] + [slice(None) for x in range( self._AXIS_LEN - 1)] new_values = self.values[slices] d = self._construct_axes_dict(self._AXIS_ORDERS[1:]) d[self._info_axis_name] = result_index result = self._constructor(new_values, **d) return result else: return self._get_item_cache(key)
def get_value(self, series, key): """ we always want to get an index value, never a value """ if not lib.isscalar(key): raise InvalidIndexError from pandas.core.indexing import maybe_droplevels from pandas.core.series import Series k = com._values_from_object(key) loc = self.get_loc(k) new_values = com._values_from_object(series)[loc] if lib.isscalar(new_values) or new_values is None: return new_values new_index = self[loc] new_index = maybe_droplevels(new_index, k) return Series(new_values, index=new_index, name=series.name)
def get_value(self, series, key): """ we always want to get an index value, never a value """ if not np.isscalar(key): raise InvalidIndexError from pandas.core.indexing import maybe_droplevels from pandas.core.series import Series k = com._values_from_object(key) loc = self.get_loc(k) new_values = com._values_from_object(series)[loc] if np.isscalar(new_values) or new_values is None: return new_values new_index = self[loc] new_index = maybe_droplevels(new_index, k) return Series(new_values, index=new_index, name=series.name)