예제 #1
0
 def _setitem_key(self, colkey, rowkey, values):
     if is_none_alike(rowkey) and len(values) != len(self):
         raise ValueError(
             "Length of values (%d) different than length of table (%d)" %
             (len(values), len(self)))
     column = self._column(colkey)
     if is_none_alike(rowkey):
         column[:] = values
     else:
         column[rowkey] = values
예제 #2
0
 def _setitem_key(self, colkey: ColIndexer, rowkey: Indexer,
                  values: Any) -> None:
     if is_none_alike(rowkey) and len(values) != len(self):
         raise ValueError("Length of values (%d) different "
                          "than length of table (%d)" %
                          (len(values), len(self)))
     column = self._column(colkey)
     if is_none_alike(rowkey):
         column[self.index] = values
     else:
         column[rowkey] = values
예제 #3
0
 def _make_columndict_projection(
     self,
     cols: Union[None, slice, int, str, List[str],
                 List[Union[int, np.integer[Any]]]],
 ) -> Dict[str, int]:
     if is_none_alike(cols):
         return self._columndict
     if isinstance(cols, slice):
         assert is_int(cols.start)  # for the moment ...
         nsl = norm_slice(cols, stop=len(self._columndict))
         return dict({
             k: v
             for (i, (k, v)) in enumerate(self._columndict.items())
             if i in range(*nsl.indices(nsl.stop))
         })
     if isinstance(cols, (int, integer_types)) or isinstance(cols, str):
         cols = [cols]  # type: ignore
     if isinstance(cols, Iterable):
         if all_int(cols):
             return dict({
                 k: v
                 for (i, (k, v)) in enumerate(self._columndict.items())
                 if i in cols
             })
         if all_string(cols):
             return dict(
                 {k: v
                  for (k, v) in self._columndict.items() if k in cols})
     raise ValueError(f"Invalid column projection {cols}")
예제 #4
0
    def _setitem_iterable(self, colkey: Iterable[Union[int, str]],
                          rowkey: Indexer, values: Any) -> None:
        # pylint: disable=too-many-branches
        colnames: List[Union[int, str]] = list(colkey)
        len_colnames = len(colnames)
        if not isinstance(values, Iterable):
            values = np.repeat(values, len_colnames)
        if isinstance(values, Mapping):
            for (k, v) in values.items():
                column = self._column(k)
                if is_none_alike(rowkey):
                    column[self.index] = v
                else:
                    column[rowkey] = v
        elif hasattr(values, "shape"):
            shape = values.shape
            if len(shape) > 1 and shape[1] != self.width(colnames):
                # and not isinstance(values, BaseTable):
                raise ValueError("Shape [1] (width)) of columns and "
                                 "value shape do not match")

            if rowkey is None:
                rowkey = self.index.to_slice_maybe()  # slice(None, None)
            for i, colname in enumerate(colnames):
                column = self._column(colname)
                if len(column.shape) > 1:
                    wid = column.shape[1]
                    column[rowkey, 0:wid] = values[i:i + wid]
                else:  # i.e. len(column.shape) == 1
                    if isinstance(values, BaseTable):
                        column[rowkey] = values[i]
                    elif len(shape) == 1:  # values is a row
                        column[rowkey] = values[i]
                    else:
                        column[rowkey] = values[:, i]
        else:
            for i, colname, v in zip(range(len_colnames), colnames, values):
                column = self._column(colname)
                if is_none_alike(rowkey):
                    column[self.index] = v
                else:
                    column[rowkey] = values[i]
예제 #5
0
    def _setitem_iterable(self, colkey, rowkey, values):
        # pylint: disable=too-many-branches
        colnames = list(colkey)
        len_colnames = len(colnames)
        if not isinstance(values, Iterable):
            values = np.repeat(values, len_colnames)
        if isinstance(values, Mapping):
            for (k, v) in six.iteritems(values):
                column = self._column(k)
                if is_none_alike(rowkey):
                    column[:] = v
                else:
                    column[rowkey] = v
        elif hasattr(values, 'shape'):
            shape = values.shape
            if len(shape) > 1 and shape[1] != self.width(colnames):
                #and not isinstance(values, BaseTable):
                raise ValueError(
                    'Shape [1] (width)) of columns and value shape do not match'
                )

            if rowkey is None:
                rowkey = slice(None, None)
            for i, colname in enumerate(colnames):
                column = self._column(colname)
                if len(column.shape) > 1:
                    wid = column.shape[1]
                    column[rowkey, 0:wid] = values[i:i + wid]
                else:  # i.e. len(column.shape) == 1
                    if isinstance(values, BaseTable):
                        column[rowkey] = values[i]
                    elif len(shape) == 1:  # values is a row
                        column[rowkey] = values[i]
                    else:
                        column[rowkey] = values[:, i]
        else:
            for i, colname, v in zip(range(len_colnames), colnames, values):
                column = self._column(colname)
                if is_none_alike(rowkey):
                    column[:] = v
                else:
                    column[rowkey] = values[i]
예제 #6
0
 def id_to_index(self, loc, as_slice=True):
     if self._is_identity:
         if isinstance(loc, slice):  # slices are inclusive
             if loc.stop != None:
                 return slice(loc.start, loc.stop + 1, loc.step)
         if is_none_alike(loc):
             loc = slice(0, self.size, 1)
         elif isinstance(loc, integer_types):
             if loc < 0:
                 loc += self._last_id
         return loc
     if self._ids_dict is None:
         self._update_ids_dict()
     if is_none_alike(loc):  # return everything
         ret = bitmap(range(0, self.size))
         if self._freelist:
             ret -= self._freelist
         if as_slice:
             ret = ret.to_slice_maybe()
         return ret
     if isinstance(loc, np.ndarray) and loc.dtype == np.int:
         ret = self._ids_dict.get_items(loc)
     elif isinstance(loc, integer_types):
         if loc < 0:
             loc = self._last_id + loc
         return self._ids_dict[loc]
     elif isinstance(loc, Iterable):
         try:
             count = len(loc)
             # pylint: disable=bare-except
         except:
             count = -1
         ret = np.fromiter(loc, dtype=np.int64, count=count)
         ret = self._ids_dict.get_items(ret)
     elif isinstance(loc, slice):
         loc_stop = self.last_id if loc.stop is None else loc.stop + 1
         ret = np.array(range(loc.start, loc_stop, loc.step or 1),
                        dtype=np.int64)
         ret = self._ids_dict.get_items(ret)
     else:
         raise ValueError('id_to_index not implemented for id "%s"' % loc)
     return indices_to_slice(ret) if as_slice else ret
예제 #7
0
    def make_projection(self, cols: Optional[List[str]],
                        index: Any) -> Tuple[List[BaseColumn], Dict[str, int]]:
        from .column_selected import ColumnSelectedView, ColumnComputedView

        dict_ = self._make_columndict_projection(cols)
        columns: List[BaseColumn] = [
            ColumnSelectedView(base=c, index=index)
            for (i, c) in enumerate(self._columns) if i in dict_.values()
        ]
        columndict: Dict[str, int] = dict(zip(dict_.keys(), range(len(dict_))))
        if not self.computed:
            return columns, columndict
        cols_as_set: Set = set()
        if is_none_alike(cols):
            cols_as_set = set(self.computed.keys())
        elif isinstance(cols, str):
            cols_as_set = set([cols])
        elif isinstance(cols, Iterable) and all_string(cols):
            cols_as_set = set(cols)
        else:
            logger.warning(
                f"computed columns will be ignored with selection {cols}")
        comp_cols: List[BaseColumn] = [
            ColumnComputedView(base=self._columns[self._columndict[c]],
                               index=index,
                               aka=aka,
                               func=func)
            for (aka, (c, func)) in self.computed.items()
            if aka in cols_as_set and aka not in self._columndict.keys()
        ]
        columns += comp_cols
        cc_dict: Dict[str, int] = {
            k: i
            for (i, k) in enumerate(
                set(self.computed.keys()) & cols_as_set, len(dict_))
        }
        columndict.update(cc_dict)
        return columns, columndict
예제 #8
0
 def __delitem__(self, key: Indexer) -> None:
     index, col_key, raw_index = self.parse_key_to_bitmap(key)
     if not is_none_alike(col_key):
         raise ValueError('Cannot delete key "%s"' % key)
     self._table.drop(index, raw_index)