def from_columns(cls, list_of_columns, names=None): ncol = get_length(list_of_columns) if names is None: names = _get_generic_names(ncol) if ncol == get_length(names): items = [(names[j], list_of_columns[j]) for j in range(ncol)] return cls.from_items(items) else: msg = 'number of names do not match the number of columns' raise ValueError(msg)
def from_rows(cls, list_of_rows, names=None): nrow = get_length(list_of_rows) ncol_per_row = [get_length(row) for row in list_of_rows] if is_list_same(ncol_per_row): if len(ncol_per_row) == 0: ncol = 0 else: ncol = ncol_per_row[0] if names is None: names = _get_generic_names(ncol) if ncol == get_length(names): items = [(names[j], [list_of_rows[i][j] for i in range(nrow)]) for j in range(ncol)] return cls.from_items(items) else: msg = 'number of names do not match the number of columns' raise ValueError(msg) else: msg = 'all rows do not have the same number of columns' raise ValueError(msg)
def names(self, value): if len(self._names) == get_length(value): if is_iterable_string(value): self._names = Array(value) self._update_names_to_index() else: msg = 'non string names are not allowed' raise ValueError(msg) else: msg = 'number of names must match the number of columns' raise ValueError(msg)
def __mod__(self, other): if is_scalar(other): return Array([__mod__(e, other) for e in self]) elif isinstance(other, Iterable): if len(self) == get_length(other): return Array([__mod__(x, y) for x, y in zip(self, other)]) else: msg = 'iterables have different lengths' raise ValueError(msg) else: msg = 'cannot perform this operation with {} object' raise ValueError(msg.format(type(object)))
def _convert_logical_index_to_int_index(self, key): assert infer_dtype(key) is bool if get_length(key) == len(self): if any([e is None for e in key]): msg = 'logical index contains missing values (None)' raise IndexError(msg) else: key = [i for i, k in enumerate(key) if k] else: msg = 'logical index does not match array length' raise IndexError(msg) return key
def from_shape(cls, shape, names=None): if get_length(shape) == 2: if is_iterable_integer(shape): if (shape[0] >= 0) and (shape[1] >= 0): if names is None: names = _get_generic_names(shape[1]) if shape[1] == get_length(names): items = [(names[j], [None for i in range(shape[0])]) for j in range(shape[1])] return cls.from_items(items) else: msg = ('number of names do not match the number ' 'of columns') raise ValueError(msg) else: msg = 'shape elements must be non-negative' raise ValueError(msg) else: msg = 'shape elements must be integers' raise ValueError(msg) else: msg = 'shape must have exactly two elements' raise ValueError(msg)
def _parse_colkey(self, colkey): # FIXME: self._data can handle slices. Should I not convert slice to # list of ints here? if isinstance(colkey, slice): colkey = range(*colkey.indices(len(self))) if is_iterable_string(colkey): colkey = [self._names_to_index[k] for k in colkey] if infer_dtype(colkey) is bool: if any([k is None for k in colkey]): msg = 'logical index contains missing values (None(s))' raise IndexError(msg) else: if get_length(colkey) == len(self): colkey = [i for i, k in enumerate(colkey) if k] else: msg = 'logical index does not match number of columns' raise IndexError(msg) valid = range(self._ncol) if not all([k in valid for k in colkey]): msg = 'invalid column key' raise KeyError(msg) return colkey