def _parseids(self, ids): if isinstance(ids, tuple): raise IndexError('too many dimensions for array') if (listable(ids) and checkany(ids, isstring)) or isstring(ids): return self.idsof(ids, safe=False) if listable(ids) and checkany(ids, lambda v: isinstance(v, bool)): return np.where(ids)[0] if ids is None: return slice(None) return ids
def delete(self, pos: Indices2D, axis: Optional[int] = 0, inline: bool = False) -> StructuredArray: narr = self if inline else self.copy() if isstring(pos): del narr._arrs[pos] return narr sids, aids = self._parseids(pos, axis=axis, mapslice=False) slic = isinstance(sids, slice) and sids == slice(None) alic = isinstance(aids, slice) and aids == slice(None) and (missing(axis) or axis == 0) if slic and alic: narr._arrs = OrderedDict() narr._length = None elif slic and not alic: if listable(aids) and len(aids) == 1 and aids[0] < 0: aids = aids[ 0] # fix the issue that currently negative indices are ignored by np.delete for k, v in narr._arrs.items(): narr._arrs[k] = np.delete(v, aids) narr._length = len(narr._arrs[l(narr._arrs.keys())[0]]) elif not slic and alic: if isinstance(sids, slice) or sids.dtype.kind not in ('S', 'U'): sids = narr.names[sids] for k in sids: del narr._arrs[k] else: raise IndexError('unable to delete portion of the array') return narr
def append(self, value: Union[str, Iterable[str]], inline: bool = False) -> NamedIndex: val = self._parsevals(value) if not listable(val): val = [val] nid = self if inline else self.copy() for i, n in enumerate(val): nid._nidct[n] = nid.size + i nid._names = np.hstack([nid._names, val]) if len(nid._names) != len(nid._nidct): raise KeyError('index names not unique') return nid
def _parseids(self, idx, axis=None, mapslice=True): if missing(axis): sids, aids = (idx, slice(None)) if not isinstance(idx, tuple) else \ (idx[0], slice(None)) if len(idx) == 1 else idx else: if isinstance(idx, tuple): raise IndexError('too many dimensions for array') if axis not in (0, 1): raise IndexError('invalid axis value') sids, aids = (idx, slice(None)) if axis == 0 else (slice(None), idx) def _wrap(ids): if ids is None: return slice(None) if isinstance(ids, slice): return ids if not listable(ids): return [ids] return ids sids, aids = smap((sids, aids), _wrap) if (isinstance(sids, slice) and mapslice) or ( listable(sids) and checkany(sids, lambda x: not isstring(x))): sids = self.names[sids] return sids, aids
def _mapids(ids, names): if isinstance(ids, NamedIndex): ids = np.array(ids) if not listable(ids): ids = [ids] if not checkany(ids, isstring): return ids if missing(names): raise KeyError('table names not set') return names.idsof(ids, safe = False)
def _wrap(ids): if ids is None: return slice(None) if isinstance(ids, slice): return ids if not listable(ids): return [ids] return ids