def test_axis_map_b(self) -> None: f1 = ff.parse('s(4,4)|v(int,float)').rename('f1') f2 = ff.parse('s(4,4)|v(str)').rename('f2') f3 = ff.parse('s(4,4)|v(bool)').rename('f3') b1 = Bus.from_frames((f1, f2, f3)) with self.assertRaises(AxisInvalid): bus_to_hierarchy(b1, deepcopy_from_bus=False, axis=3, init_exception_cls=ErrorInitQuilt)
def _update_axis_labels(self) -> None: if self._axis_hierarchy is None or self._axis_opposite is None: self._axis_hierarchy, self._axis_opposite = bus_to_hierarchy( self._bus, axis=self._axis, deepcopy_from_bus=self._deepcopy_from_bus, init_exception_cls=ErrorInitQuilt, ) if self._axis == 0: if not self._retain_labels: try: self._index = self._axis_hierarchy.level_drop(1) except ErrorInitIndexNonUnique: raise self._error_update_axis_labels(self._axis) from None else: # get hierarchical self._index = self._axis_hierarchy self._columns = self._axis_opposite else: if not self._retain_labels: try: self._columns = self._axis_hierarchy.level_drop(1) except ErrorInitIndexNonUnique: raise self._error_update_axis_labels(self._axis) from None else: self._columns = self._axis_hierarchy self._index = self._axis_opposite self._assign_axis = False
def test_assertions(axis: int, flag: bool) -> None: hierarchy, opposite = bus_to_hierarchy(b1, axis=axis, deepcopy_from_bus=flag, init_exception_cls=ErrorInitBus) if axis == 0: expected_tree: tp.Dict[str, Index] = { 'f1': indices, 'f2': indices, 'f3': indices } expected_index = columns else: expected_index = indices expected_tree = {'f1': columns, 'f2': columns, 'f3': columns} self.compare_trees(hierarchy.to_tree(), expected_tree) self.assertTrue(expected_index.equals(opposite))
def test_bus_to_hierarchy_b(self) -> None: class CustomError(Exception): pass tree1 = dict(a_I=Index((1, 2, 3)), a_II=Index((1, 2, 3))) tree2 = dict(b_I=Index((1, 2, 3)), b_II=Index((1, 2, 3))) tree3 = dict(c_I=Index((1, 2, 3)), c_II=Index((1, 2, 3))) index1 = IndexHierarchy.from_tree(tree1) index2 = IndexHierarchy.from_tree(tree2) index3 = IndexHierarchy.from_tree(tree3) values = np.arange(36).reshape(6, 6) # Align all the frames on columns! f1 = Frame(values, index=index1, columns=index1, name="f1") f2 = Frame(values, index=index2, columns=index1, name="f2") f3 = Frame(values, index=index3, columns=index1, name="f3") b1 = Bus.from_frames((f1, f2, f3)) def test_assertions(hierarchy: IndexHierarchy, opposite: Index) -> None: expected_tree = dict(f1=tree1, f2=tree2, f3=tree3) self.compare_trees(hierarchy.to_tree(), expected_tree) self.assertTrue(index1.equals(opposite)) test_assertions(*bus_to_hierarchy(b1, axis=0, deepcopy_from_bus=False, init_exception_cls=CustomError)) test_assertions(*bus_to_hierarchy( b1, axis=0, deepcopy_from_bus=True, init_exception_cls=CustomError)) # Cannot do this since the frames do not share the same index with self.assertRaises(CustomError): bus_to_hierarchy(b1, axis=1, deepcopy_from_bus=False, init_exception_cls=CustomError) with self.assertRaises(CustomError): bus_to_hierarchy(b1, axis=1, deepcopy_from_bus=True, init_exception_cls=CustomError) # Align all the frames on index! f1 = Frame(values, index=index1, columns=index1, name="f1") f2 = Frame(values, index=index1, columns=index2, name="f2") f3 = Frame(values, index=index1, columns=index3, name="f3") b1 = Bus.from_frames((f1, f2, f3)) test_assertions(*bus_to_hierarchy(b1, axis=1, deepcopy_from_bus=False, init_exception_cls=CustomError)) test_assertions(*bus_to_hierarchy( b1, axis=1, deepcopy_from_bus=True, init_exception_cls=CustomError)) # Cannot do this since the frames do not share the same columns with self.assertRaises(CustomError): bus_to_hierarchy(b1, axis=0, deepcopy_from_bus=False, init_exception_cls=CustomError) with self.assertRaises(CustomError): bus_to_hierarchy(b1, axis=0, deepcopy_from_bus=True, init_exception_cls=CustomError)