def test_dtype_to_index_cls_b(self) -> None: t1 = dtype_to_index_cls(True, np.dtype(str)) self.assertEqual(t1, Index) t2 = dtype_to_index_cls(False, np.dtype(str)) self.assertEqual(t2, IndexGO) t3 = dtype_to_index_cls(True, np.dtype(float)) self.assertEqual(t3, Index) t4 = dtype_to_index_cls(False, np.dtype(float)) self.assertEqual(t4, IndexGO)
def to_index(labels: tp.Iterable[tp.Hashable], *, default_constructor: tp.Type[IndexBase], name: NameType = None, ) -> IndexBase: '''Create and return the ``Index`` based on the array ``dtype`` ''' from static_frame.core.index_datetime import dtype_to_index_cls if labels.__class__ is not np.ndarray: # we can assume that this is 1D; returns an immutable array labels, _ = iterable_to_array_1d(labels) return dtype_to_index_cls( static=default_constructor.STATIC, dtype=labels.dtype)(labels, name=name) #type: ignore
def test_dtype_to_index_cls_a(self) -> None: t1 = dtype_to_index_cls(True, np.dtype('datetime64[D]')) self.assertEqual(t1, IndexDate) t2 = dtype_to_index_cls(False, np.dtype('datetime64[D]')) self.assertEqual(t2, IndexDateGO) t3 = dtype_to_index_cls(True, np.dtype('datetime64[s]')) self.assertEqual(t3, IndexSecond) t4 = dtype_to_index_cls(False, np.dtype('datetime64[s]')) self.assertEqual(t4, IndexSecondGO) t5 = dtype_to_index_cls(True, np.dtype('datetime64[Y]')) self.assertEqual(t5, IndexYear) t6 = dtype_to_index_cls(False, np.dtype('datetime64[Y]')) self.assertEqual(t6, IndexYearGO)
def from_arrays( self, blocks: tp.Iterable[np.ndarray], *, index: tp.Optional[IndexInitializer] = None, columns: tp.Optional[IndexInitializer] = None, name: NameType = None, axis: int = 0, ) -> None: ''' Given an iterable of arrays, write out an NPZ or NPY directly, without building up intermediary :obj:`Frame`. If axis 0, the arrays are vertically stacked; if axis 1, they are horizontally stacked. For both axis, if included, indices must be of appropriate length. Args: blocks: *, index: An array, :obj:`Index`, or :obj:`IndexHierarchy`. columns: An array, :obj:`Index`, or :obj:`IndexHierarchy`. name: axis: ''' if not self._writeable: raise UnsupportedOperation('Open with mode "w" to write.') metadata: tp.Dict[str, tp.Any] = {} if isinstance(index, IndexBase): depth_index = index.depth name_index = index.name cls_index = index.__class__ ArchiveIndexConverter.index_encode( metadata=metadata, archive=self._archive, index=index, key_template_values=Label.FILE_TEMPLATE_VALUES_INDEX, key_types=Label.KEY_TYPES_INDEX, depth=depth_index, include=True, ) elif index is not None: if index.__class__ is not np.ndarray: raise RuntimeError( 'index argument must be an Index, IndexHierarchy, or 1D np.ndarray' ) depth_index = 1 name_index = None cls_index = dtype_to_index_cls(True, index.dtype) #type: ignore ArchiveIndexConverter.array_encode( metadata=metadata, archive=self._archive, array=index, key_template_values=Label.FILE_TEMPLATE_VALUES_INDEX, ) else: depth_index = 1 name_index = None cls_index = Index if isinstance(columns, IndexBase): depth_columns = columns.depth name_columns = columns.name cls_columns = columns.__class__ ArchiveIndexConverter.index_encode( metadata=metadata, archive=self._archive, index=columns, key_template_values=Label.FILE_TEMPLATE_VALUES_COLUMNS, key_types=Label.KEY_TYPES_COLUMNS, depth=depth_columns, include=True, ) elif columns is not None: if columns.__class__ is not np.ndarray: raise RuntimeError( 'index argument must be an Index, IndexHierarchy, or 1D np.ndarray' ) depth_columns = 1 # only support 1D name_columns = None cls_columns = dtype_to_index_cls(True, columns.dtype) #type: ignore ArchiveIndexConverter.array_encode( metadata=metadata, archive=self._archive, array=columns, key_template_values=Label.FILE_TEMPLATE_VALUES_COLUMNS, ) else: depth_columns = 1 # only support 1D name_columns = None cls_columns = Index metadata[Label.KEY_NAMES] = [ name, name_index, name_columns, ] # do not store Frame class as caller will determine metadata[Label.KEY_TYPES] = [ cls_index.__name__, cls_columns.__name__, ] if axis == 1: rows = 0 for i, array in enumerate(blocks): if not rows: rows = array.shape[0] else: if array.shape[0] != rows: raise RuntimeError('incompatible block shapes') self._archive.write_array(Label.FILE_TEMPLATE_BLOCKS.format(i), array) elif axis == 0: # for now, just vertically concat and write, though this has a 2X memory requirement resolved = concat_resolved(blocks, axis=0) # if this results in an obect array, an exception will be raised self._archive.write_array(Label.FILE_TEMPLATE_BLOCKS.format(0), resolved) i = 0 else: raise AxisInvalid(f'invalid axis {axis}') metadata[Label.KEY_DEPTHS] = [ i + 1, # block count depth_index, depth_columns ] self._archive.write_metadata(metadata)