def assign( self, py_obj = None ): if is_numpy_array(py_obj) or isinstance(py_obj, list): py_type = self.from_obj(py_obj) return self.assign_type(py_type) return InvalidType()
def assign( self, py_obj: t.Optional[t.Any] = None ) -> t.Union["NDArrayType", InvalidType]: if is_numpy_array(py_obj) or isinstance(py_obj, list): py_type = self.from_obj(py_obj) return self.assign_type(py_type) return InvalidType()
def _init_from_ndarray(self, ndarray, columns, optional=True, dtype=None): assert util.is_numpy_array( ndarray), "ndarray argument expects a `numpy.ndarray` object" self.data = [] self._assert_valid_columns(columns) self.columns = columns self._make_column_types(dtype, optional) for row in ndarray.tolist(): self.add_data(*row)
def numpy_arrays_to_lists(payload): """Casts all numpy arrays to lists so we don't convert them to histograms, primarily for Plotly """ for key, val in six.iteritems(payload): if isinstance(val, dict): payload[key] = numpy_arrays_to_lists(val) elif util.is_numpy_array(val): payload[key] = val.tolist() return payload
def from_obj(cls, py_obj: t.Optional[t.Any] = None) -> "NDArrayType": if is_numpy_array(py_obj): return cls(py_obj.shape) # type: ignore elif isinstance(py_obj, list): shape = [] target = py_obj while isinstance(target, list): dim = len(target) shape.append(dim) if dim > 0: target = target[0] return cls(shape) else: raise TypeError( "NDArrayType.from_obj expects py_obj to be ndarray or list, found {}" .format(py_obj.__class__))
def __init__( self, columns=None, data=None, rows=None, dataframe=None, dtype=None, optional=True, allow_mixed_types=False, ): """rows is kept for legacy reasons, we use data to mimic the Pandas api""" super(Table, self).__init__() if allow_mixed_types: dtype = _dtypes.AnyType # This is kept for legacy reasons (tss: personally, I think we should remove this) if columns is None: columns = ["Input", "Output", "Expected"] # Explicit dataframe option if dataframe is not None: self._init_from_dataframe(dataframe, columns, optional, dtype) else: # Expected pattern if data is not None: if util.is_numpy_array(data): self._init_from_ndarray(data, columns, optional, dtype) elif util.is_pandas_data_frame(data): self._init_from_dataframe(data, columns, optional, dtype) else: self._init_from_list(data, columns, optional, dtype) # legacy elif rows is not None: self._init_from_list(rows, columns, optional, dtype) # Default empty case else: self._init_from_list([], columns, optional, dtype)