def __getitem__(self, x): if x is newaxis or (isinstance(x, tuple) and newaxis in x): return self.view(type=ndarray, dtype=self.dtype)[x] if x is Ellipsis: return self.view() if isinstance(x, tuple): indices = x else: indices = (x,) ellipsis_and_ints = True orig_indices = indices for i in indices: if isinstance(i, int) or i is Ellipsis: pass else: ellipsis_and_ints = False ellips_count = len([i for i in indices if isinstance(i, type(Ellipsis))]) if ellips_count==1: i = indices.index(Ellipsis) indices = indices[:i] + (slice(None),)*(self.ndim-(len(x)-1)) + indices[i+1:] elif ellips_count > 1: raise IndexError("Can not handle more than one Ellipsis") info = self.info if len(indices)==1 and isinstance(indices[0], ValueArray) and indices[0].dtype == bool: if len(indices[0].info) == 1: idx = self.info_index(indices[0].info[0]) indices = (slice(None),)*idx + (indices[0],) olddim = indices[-1].info[0] info = list(info) info[idx] = olddim.__class__(olddim.name, olddim.data[indices[-1]]) info = tuple(info) try: indices = indices + (slice(None),)*(self.ndim - len(indices)) info = [] dim_in_indices = dict((x.info[0].name, x.info[0]) for x in indices if isinstance(x, ValueArray)) for idx, dim in zip(indices, self.info): if isinstance(idx, int): continue elif isinstance(idx, slice): info.append(dim[idx]) # print "X", idx elif isinstance(dim, DimBase): info.append(dim_in_indices.get(dim.name, dim)) else: info.append(dim) # print info #info = tuple(dim for idx, dim in zip(indices, self.info) if not isinstance(idx, int)) if ellipsis_and_ints: indices = orig_indices out = ndarray.__getitem__(self, indices) if isinstance(out, ndarray): return self.__class__(out, info=info, copy=False) else: return out except ValueArrayShapeInfoMismatchError: logging.warn("WARNING mismatch") out = ndarray.__getitem__(self, indices) return out.view(type=ndarray, dtype=self.dtype)
def __getitem__(self, indx): reset_full = True if isinstance(indx, Date): indx = self.find_dates(indx) reset_full = False elif numeric.asarray(indx).dtype.kind == 'O': try: indx = self.find_dates(indx) except AttributeError: pass r = ndarray.__getitem__(self, indx) if isinstance(r, (generic, int)): return Date(self.freq, value=r) elif hasattr(r, 'size') and r.size == 1: # need to check if it has a size attribute for situations # like when the datearray is the data for a maskedarray # or some other subclass of ndarray with wierd getitem # behaviour return Date(self.freq, value=r.item()) else: if hasattr(r, '_cachedinfo'): _cache = r._cachedinfo _cache.update(dict([(k,_cache[k][indx]) for k in ('toobj', 'tostr', 'toord') if _cache[k] is not None])) _cache['steps'] = None if reset_full: _cache['full'] = None _cache['hasdups'] = None return r
def __getitem__(self, ind): nInd = self._interpretIndexes(ind) a = ndarray.__getitem__(self, nInd) if type(a) == type(self): ## generate new info array a._info = [] for i in range(0, len(nInd)): ## iterate over all axes if isinstance(nInd[i], (slice, list)): ## If the axis is sliced, keep the info but chop if necessary a._info.append(self._axisSlice(i, nInd[i])) a._info.append(self._info[-1]) ## Tack on extra data return a
def __getitem__(self, ind): nInd = self._interpretIndexes(ind) a = ndarray.__getitem__(self, nInd) if type(a) == type(self): ## generate new info array a._info = [] for i in range(0, len(nInd)): ## iterate over all axes if type(nInd[i]) == types.SliceType or type( nInd[i] ) == types.ListType: ## If the axis is sliced, keep the info but chop if necessary a._info.append(self._axisSlice(i, nInd[i])) a._info.append(self._info[-1]) ## Tack on extra data return a
def __getitem__(self, key): #print("__getitem__") # if all slices, be smart if (isinstance(key, tuple) and len(key) == self.ndim and len(key) > 0 and all(isinstance(_, slice) for _ in key)): res = ndarray.__getitem__(self, key) starts, _, stride = list(zip(*[slc.indices(n) for slc, n in zip(key, self.shape)])) res.axis_offsets = (asarray(self.axis_offsets) + asarray(starts) * asarray(self.voxel_size)) res.voxel_size = asarray(self.voxel_size) * asarray(stride) return res # if not, then down cast first else: return self.view(ndarray)[key]
def __getitem__(self, indx): reset_full = True keep_chrono = False # Determine what kind of index is used if isinstance(indx, Date): # indx = self.find_dates(indx) # indx = int(self.find_dates(indx)[0]) indx = self.date_to_index(indx) reset_full = False elif isinstance(indx, slice): keep_chrono = True elif np.asarray(indx).dtype.kind == 'O': try: indx = self.find_dates(indx) except AttributeError: pass # Select the data r = ndarray.__getitem__(self, indx) # Case 1. A simple integer if isinstance(r, (generic, int)): return Date(self.freq, value=r) elif not getattr(r, 'ndim', 1): # need to check if it has a ndim attribute for situations # like when the datearray is the data for a maskedarray # or some other subclass of ndarray with wierd getitem # behaviour return Date(self.freq, value=r.item()) else: if hasattr(r, '_cachedinfo'): _cache = r._cachedinfo # Select the appropriate cached representations _cache.update( dict([(k, _cache[k][indx]) for k in ('toobj', 'tostr', 'toord') if _cache[k] is not None])) # Reset the ischrono flag if needed if not (keep_chrono and _cache['ischrono']): _cache['ischrono'] = None # Reset the sorting indices _cache['chronidx'] = None # Reset the steps _cache['steps'] = None if reset_full: _cache['full'] = None _cache['hasdups'] = None return r
def __getitem__(self, indx): reset_full = True keep_chrono = False # Determine what kind of index is used if isinstance(indx, Date): # indx = self.find_dates(indx) # indx = int(self.find_dates(indx)[0]) indx = self.date_to_index(indx) reset_full = False elif isinstance(indx, slice): keep_chrono = True elif np.asarray(indx).dtype.kind == 'O': try: indx = self.find_dates(indx) except AttributeError: pass # Select the data r = ndarray.__getitem__(self, indx) # Case 1. A simple integer if isinstance(r, (generic, int)): return Date(self.freq, value=r) elif not getattr(r, 'ndim', 1): # need to check if it has a ndim attribute for situations # like when the datearray is the data for a maskedarray # or some other subclass of ndarray with wierd getitem # behaviour return Date(self.freq, value=r.item()) else: if hasattr(r, '_cachedinfo'): _cache = r._cachedinfo # Select the appropriate cached representations _cache.update(dict([(k, _cache[k][indx]) for k in ('toobj', 'tostr', 'toord') if _cache[k] is not None])) # Reset the ischrono flag if needed if not (keep_chrono and _cache['ischrono']): _cache['ischrono'] = None # Reset the sorting indices _cache['chronidx'] = None # Reset the steps _cache['steps'] = None if reset_full: _cache['full'] = None _cache['hasdups'] = None return r
def get(self, key, default=None): """ Returns value occupying requested index, default to specified missing value if not present Parameters ---------- key : object Index value looking for default : object, optional Value to return if key not in index Returns ------- y : scalar """ if key in self.index: return ndarray.__getitem__(self, self.index.indexMap[key]) else: return default
def __getitem__(self, x): if x is newaxis or (isinstance(x, tuple) and newaxis in x): return self.view(type=ndarray, dtype=self.dtype)[x] if x is Ellipsis: return self.view() if isinstance(x, tuple): indices = x else: indices = (x,) ellipsis_and_ints = True orig_indices = indices for i in indices: if isinstance(i, integer_types) or i is Ellipsis: pass else: ellipsis_and_ints = False ellips_count = len([i for i in indices if isinstance(i, type(Ellipsis))]) if ellips_count == 1: i = indices.index(Ellipsis) indices = (indices[:i] + (slice(None),) * (self.ndim - (len(x) - 1)) + indices[i + 1:]) elif ellips_count > 1: raise IndexError("Can not handle more than one Ellipsis") dims = self.dims testbool = (len(indices) == 1 and isinstance(indices[0], hfarray) and indices[0].dtype == bool) if testbool: reorder = [] for dim in self.dims: if dim not in indices[0].dims: reorder.append(dim) else: break reorder2 = reorder + list(indices[0].dims) reordered = self.reorder_dimensions(*reorder2) if len(indices[0].dims) == 1: dim = indices[0].dims[0] newdim = dim.__class__(dim.name, np.array(dim.data)[indices[0]], unit=dim.unit) else: newdim = get_new_anonymous_dim(self, int(np.sum(indices[0]))) dims = (reordered.dims[:len(reorder)] + (newdim,) + reordered.dims[len(reorder) + len(indices[0].dims):]) idx = (slice(None), ) * len(reorder) + (indices[0],) return hfarray(ndarray.__getitem__(reordered, idx), dims=dims) indices = indices + (slice(None),) * (self.ndim - len(indices)) dims = [] dim_in_indices = dict((x.dims[0].name, x.dims[0]) for x in indices if isinstance(x, hfarray)) for idx, dim in zip(indices, self.dims): if isinstance(idx, integer_types): continue elif isinstance(idx, slice): dims.append(dim[idx]) else: dims.append(dim_in_indices.get(dim.name, dim)) if ellipsis_and_ints: indices = orig_indices out = ndarray.__getitem__(self, indices) if isinstance(out, ndarray): return self.__class__(out, dims=dims, copy=False) else: return out
def __getitem__(self, i): if isinstance(i, basestring): scale_factor = self.units[i][0] return self * scale_factor if i is None: return self return ndarray.__getitem__(self, i)