def test_index_level_dtypes_a(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) targets = ArrayGO((IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level0 = IndexLevelGO(index=groups, targets=targets) self.assertEqual([d.kind for d in level0.dtypes()], ['U', 'U'])
def test_index_level_append_b(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) lvl2a = IndexLevelGO(index=observations) lvl2b = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2a, lvl2b)) lvl1a = IndexLevelGO(index=groups, targets=lvl2_targets, offset=0) with self.assertRaises(RuntimeError): lvl1a.append((1, 2, 3)) lvl1a.append((1, 2)) self.assertEqual( lvl1a.values.tolist(), [['A', 'x'], ['A', 'y'], ['B', 'x'], ['B', 'y'], [1, 2]])
def test_index_level_a(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) targets = np.array( (IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level0 = IndexLevelGO(index=groups, targets=ArrayGO(targets)) level1 = level0.to_index_level() groups = IndexGO(('C', 'D')) observations = IndexGO(('x', 'y', 'z')) targets = np.array( (IndexLevelGO(index=observations), IndexLevelGO(observations, offset=3))) level2 = IndexLevelGO(index=groups, targets=ArrayGO(targets)) with self.assertRaises(RuntimeError): level0.extend(IndexLevelGO(index=observations)) level0.extend(level2) self.assertEqual(len(level0.values), 10) self.assertEqual(len(level1.values), 4) self.assertEqual(len(level2.values), 6) assert level0.targets is not None self.assertEqual([lvl.offset for lvl in level0.targets], [0, 2, 4, 7]) self.assertEqual(level2.depth, next(level2.depths()))
def test_index_level_deepcopy_a(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) lvl2a = IndexLevelGO(index=observations) lvl2b = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2a, lvl2b)) assert id(lvl2a.index) != id(lvl2b.index) lvl1a = IndexLevelGO(index=groups, targets=lvl2_targets, offset=0) post = copy.deepcopy(lvl1a) self.assertEqual(lvl1a.values.tolist(), post.values.tolist()) self.assertEqual( id(post.targets[0].index._labels), #type: ignore id(post.targets[1].index._labels)) #type: ignore
def test_index_level_extend_d(self) -> None: tree = { "I": {"A": (1, 2), "B": (1, 2, 3), "C": (2, 3)}, "II": {"D": (1, 2, 3), "E": (1,)}, } lvl0 = IndexLevelGO.from_tree(tree) lvl1 = IndexLevelGO(index=IndexGO(()), targets=None, depth_reference=3) # RuntimeError: found IndexLevel with None as targets with self.assertRaises(RuntimeError): lvl1.extend(lvl0)
def test_index_level_a(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) targets = np.array((IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level0 = IndexLevelGO(index=groups, targets=ArrayGO(targets)) level1 = level0.to_index_level() groups = IndexGO(('C', 'D')) observations = IndexGO(('x', 'y', 'z')) targets = np.array((IndexLevelGO(index=observations), IndexLevelGO(observations, offset=3))) level2 = IndexLevelGO(index=groups, targets=ArrayGO(targets)) level0.extend(level2) self.assertEqual(len(level0.get_labels()), 10) self.assertEqual(len(level1.get_labels()), 4) self.assertEqual(len(level2.get_labels()), 6) assert level0.targets is not None # import ipdb; ipdb.set_trace() self.assertEqual([lvl.offset for lvl in level0.targets], [0, 2, 4, 7])
def test_index_level_append_a(self) -> None: category = IndexGO(('I', 'II')) groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) lvl2a = IndexLevelGO(index=observations) lvl2b = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2a, lvl2b)) # must defensively copy index assert id(lvl2a.index) != id(lvl2b.index) lvl1a = IndexLevelGO(index=groups, targets=lvl2_targets, offset=0) # must create new index levels for each lower node lvl2c = IndexLevelGO(index=observations) lvl2d = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2c, lvl2d)) # must defensively copy index assert id(lvl2c.index) != id(lvl2d.index) lvl1b = IndexLevelGO(index=groups, targets=lvl2_targets, offset=len(lvl1a)) # we need as many targets as len(index) lvl0 = IndexLevelGO(index=category, targets=ArrayGO((lvl1a, lvl1b))) with self.assertRaises(RuntimeError): # RuntimeError: level for extension does not have necessary levels. lvl0.extend(lvl1b) lvl0.append(('II', 'B', 'z')) # depth not found is 2 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.values], list(range(9))) lvl0.append(('II', 'C', 'a')) # depth not found is 1 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.values], list(range(10))) lvl0.append(('III', 'A', 'a')) # 0 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.values], list(range(11))) self.assertEqual( lvl0.values.tolist(), [['I', 'A', 'x'], ['I', 'A', 'y'], ['I', 'B', 'x'], ['I', 'B', 'y'], ['II', 'A', 'x'], ['II', 'A', 'y'], ['II', 'B', 'x'], ['II', 'B', 'y'], ['II', 'B', 'z'], ['II', 'C', 'a'], ['III', 'A', 'a']])
def test_level_append_a(self): category = IndexGO(('I', 'II')) groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) lvl2a = IndexLevelGO(index=observations) lvl2b = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2a, lvl2b)) # must defensively copy index assert id(lvl2a.index) != id(lvl2b.index) lvl1a = IndexLevelGO(index=groups, targets=lvl2_targets, offset=0) # must create new index levels for each lower node lvl2c = IndexLevelGO(index=observations) lvl2d = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2c, lvl2d)) # must defensively copy index assert id(lvl2c.index) != id(lvl2d.index) lvl1b = IndexLevelGO(index=groups, targets=lvl2_targets, offset=len(lvl1a)) # we need as many targets as len(index) lvl0 = IndexLevelGO(index=category, targets=ArrayGO((lvl1a, lvl1b))) lvl0.append(('II', 'B', 'z')) # depth not found is 2 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.get_labels()], list(range(9))) lvl0.append(('II', 'C', 'a')) # depth not found is 1 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.get_labels()], list(range(10))) lvl0.append(('III', 'A', 'a')) # 0 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.get_labels()], list(range(11))) self.assertEqual( lvl0.get_labels().tolist(), [['I', 'A', 'x'], ['I', 'A', 'y'], ['I', 'B', 'x'], ['I', 'B', 'y'], ['II', 'A', 'x'], ['II', 'A', 'y'], ['II', 'B', 'x'], ['II', 'B', 'y'], ['II', 'B', 'z'], ['II', 'C', 'a'], ['III', 'A', 'a']])
def test_index_level_append_c(self) -> None: levels1 = IndexLevelGO(IndexGO(()), depth_reference=3) levels1.append(('III', 'A', 1)) self.assertEqual(len(tuple(levels1.dtypes_at_depth(0))), 1) self.assertEqual(len(tuple(levels1.dtypes_at_depth(1))), 1) self.assertEqual(len(tuple(levels1.dtypes_at_depth(2))), 1) self.assertEqual(levels1.values.tolist(), [['III', 'A', 1]]) levels1.append(('III', 'A', 2)) self.assertEqual(levels1.values.tolist(), [['III', 'A', 1], ['III', 'A', 2]])
def test_index_level_extend_c(self) -> None: observations0 = IndexGO(('x', 'y')) targets0 = ArrayGO( (IndexLevelGO(index=observations0), IndexLevelGO(observations0, offset=2))) level0 = IndexLevelGO(index=IndexDateGO(('2021', '2022')), targets=targets0) observations1 = IndexGO(('x', 'y')) targets1 = ArrayGO( (IndexLevelGO(index=observations1), IndexLevelGO(observations1, offset=2))) level1 = IndexLevelGO(index=IndexYearMonthGO(('1853-03', '1873-12')), targets=targets1) # RuntimeError: level for extension does not have corresponding types: <class 'static_frame.core.index_datetime.IndexDateGO'>, <class 'static_frame.core.index_datetime.IndexYearMonthGO'> with self.assertRaises(RuntimeError): level0.extend(level1)
def test_index_level_get_labels_a(self): groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) targets = ArrayGO( (IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level0 = IndexLevelGO(index=groups, targets=targets) self.assertEqual(level0.get_labels().tolist(), [['A', 'x'], ['A', 'y'], ['B', 'x'], ['B', 'y']]) self.assertEqual(level0.get_labels().dtype.kind, 'U') groups = IndexGO((1, 2)) observations = IndexGO((10, 20)) targets = ArrayGO( (IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level1 = IndexLevelGO(index=groups, targets=targets) self.assertEqual(level1.get_labels().dtype.kind, 'i')
def test_index_level_extend_b(self) -> None: level0 = IndexLevelGO(index=IndexGO(('a', 'b')), targets=None) level1 = IndexLevelGO(index=IndexGO(('c', 'd')), targets=None) # RuntimeError: found IndexLevel with None as targets with self.assertRaises(RuntimeError): level0.extend(level1)
def test_index_level_extend_a(self) -> None: level0 = IndexLevelGO(index=IndexGO(('a', 'b')), targets=None) with self.assertRaises(RuntimeError): level0.extend(level0)