Ejemplo n.º 1
0
    def test_hierarchy_frame_b(self):
        OD = OrderedDict
        tree = OD([
                ('I', OD([
                        ('A', (1,)), ('B', (1, 2))
                        ])
                ),
                ('II', OD([
                        ('A', (1,)), ('B', (1, 2))
                        ])
                ),
                ])

        ih = IndexHierarchyGO.from_tree(tree)
        data = np.arange(6*6).reshape(6, 6)
        # TODO: this only works if own_columns is True for now
        f1 = FrameGO(data, index=range(6), columns=ih, own_columns=True)
        f1[('II', 'B', 3)] = 0

        f2 = f1[HLoc[:, 'B']]
        self.assertEqual(f2.shape, (6, 5))
        self.assertEqual(f2.to_pairs(0),
                ((('I', 'B', 1), ((0, 1), (1, 7), (2, 13), (3, 19), (4, 25), (5, 31))), (('I', 'B', 2), ((0, 2), (1, 8), (2, 14), (3, 20), (4, 26), (5, 32))), (('II', 'B', 1), ((0, 4), (1, 10), (2, 16), (3, 22), (4, 28), (5, 34))), (('II', 'B', 2), ((0, 5), (1, 11), (2, 17), (3, 23), (4, 29), (5, 35))), (('II', 'B', 3), ((0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0))))
                )

        f3 = f1[HLoc[:, :, 1]]
        self.assertEqual(f3.to_pairs(0), ((('I', 'A', 1), ((0, 0), (1, 6), (2, 12), (3, 18), (4, 24), (5, 30))), (('I', 'B', 1), ((0, 1), (1, 7), (2, 13), (3, 19), (4, 25), (5, 31))), (('II', 'A', 1), ((0, 3), (1, 9), (2, 15), (3, 21), (4, 27), (5, 33))), (('II', 'B', 1), ((0, 4), (1, 10), (2, 16), (3, 22), (4, 28), (5, 34)))))


        f4 = f1.loc[[2, 5], HLoc[:, 'A']]
        self.assertEqual(f4.to_pairs(0),
                ((('I', 'A', 1), ((2, 12), (5, 30))), (('II', 'A', 1), ((2, 15), (5, 33)))))
Ejemplo n.º 2
0
    def test_hierarchy_index_go_a(self):

        OD = OrderedDict
        tree1 = OD([
            ('I', OD([('A', (1, )), ('B', (1, 2))])),
            ('II', OD([('A', (1, )), ('B', (1, 2))])),
        ])
        ih1 = IndexHierarchyGO.from_tree(tree1)

        tree2 = OD([
            ('III', OD([('A', (1, )), ('B', (1, 2))])),
            ('IV', OD([('A', (1, )), ('B', (1, 2))])),
        ])
        ih2 = IndexHierarchyGO.from_tree(tree2)

        ih1.extend(ih2)

        # self.assertEqual(ih1.loc_to_iloc(('IV', 'B', 2)), 11)
        self.assertEqual(len(ih2), 6)

        # need tuple here to distinguish from iterable type selection
        self.assertEqual([ih1.loc_to_iloc(tuple(v)) for v in ih1.values],
                         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Ejemplo n.º 3
0
    def test_hierarchy_keys_a(self) -> None:
        OD = OrderedDict
        tree = OD([
            ('I', OD([('A', (1, 2)), ('B', (1, 2))])),
            ('II', OD([('A', (1, 2)), ('B', (1, 2))])),
        ])

        ih = IndexHierarchyGO.from_tree(tree)

        # NOTE: for now, __iter__ return arrays, so we have convert to a tuple
        self.assertEqual([tuple(k) in ih.keys() for k in ih],
                         [True, True, True, True, True, True, True, True])

        ih.append(('III', 'A', 1))

        self.assertEqual(
            ih.keys(), {('I', 'B', 1), ('I', 'A', 2), ('II', 'B', 2),
                        ('II', 'A', 2), ('I', 'A', 1), ('III', 'A', 1),
                        ('II', 'B', 1), ('II', 'A', 1), ('I', 'B', 2)})