def test_index_levels_equals_b(self) -> None:

        idx1 = Index(('a', 'b', 'c', 'd', 'e'))
        idx2 = Index(range(10))
        levels1 = IndexHierarchy.from_product(idx1, idx2)._levels

        idx3 = Index(('a', 'b', 'c', 'd', 'e'))
        idx4 = Index(range(10))
        levels2 = IndexHierarchy.from_product(idx3, idx4)._levels

        self.assertTrue(levels1.equals(levels2))
    def test_index_hierarchy_pickle_a(self) -> None:

        a = IndexHierarchy.from_product((10, 20), (3, 7))
        b = IndexHierarchy.from_product(('a', 'b'), ('x', 'y'))

        for index in (a, b):
            # force creating of ._labels
            self.assertTrue(len(index.values), len(index))

            pbytes = pickle.dumps(index)
            index_new = pickle.loads(pbytes)

            for v in index:  # iter labels (arrays here)
                self.assertFalse(index_new._labels.flags.writeable)
                self.assertEqual(index_new.loc[tuple(v)], index.loc[tuple(v)])
    def test_index_hierarchy_sort_a(self) -> None:

        ih1 = IndexHierarchy.from_product((1, 2), (30, 70))

        self.assertEqual(
            ih1.sort(ascending=False).values.tolist(),
            [[2, 70], [2, 30], [1, 70], [1, 30]])
    def test_hierarchy_extract_a(self) -> None:
        idx = IndexHierarchy.from_product(['A', 'B'], [1, 2])

        self.assertEqual(idx.iloc[1], ('A', 2))
        self.assertEqual(idx.loc[('B', 1)], ('B', 1))
        self.assertEqual(idx[2], ('B', 1))  #pylint: disable=E1136
        self.assertEqual(idx.loc[HLoc['B', 1]], ('B', 1))
    def test_frame_iter_element_c(self) -> None:

        a2 = np.array([[None, None], [None, 1], [None, 5]], dtype=object)
        a1 = np.array([True, False, True])
        a3 = np.array([['a'], ['b'], ['c']])

        tb1 = TypeBlocks.from_blocks((a3, a1, a2))

        f1 = Frame(tb1,
                   index=self.get_letters(None, tb1.shape[0]),
                   columns=IndexHierarchy.from_product(('i', 'ii'),
                                                       ('a', 'b')))
        values = list(f1.iter_element())
        self.assertEqual(
            values,
            ['a', True, None, None, 'b', False, None, 1, 'c', True, None, 5])

        f2 = f1.iter_element().apply(lambda x: str(x).lower().replace('e', ''))

        self.assertEqual(
            f1.columns.__class__,
            f2.columns.__class__,
        )

        self.assertEqual(
            f2.to_pairs(0),
            ((('i', 'a'), (('a', 'a'), ('b', 'b'), ('c', 'c'))),
             (('i', 'b'), (('a', 'tru'), ('b', 'fals'), ('c', 'tru'))),
             (('ii', 'a'), (('a', 'non'), ('b', 'non'), ('c', 'non'))),
             (('ii', 'b'), (('a', 'non'), ('b', '1'), ('c', '5')))))
    def test_frame_iter_group_labels_b(self) -> None:

        records = (
            (2, 2, 'a', 'q', False, False),
            (30, 34, 'b', 'c', True, False),
            (2, 95, 'c', 'd', False, False),
        )
        f1 = Frame.from_records(records,
                                columns=IndexHierarchy.from_product(
                                    (1, 2, 3), ('a', 'b')),
                                index=('x', 'y', 'z'))

        # with axis 1, we are grouping based on columns while maintain the index
        post_tuple = tuple(f1.iter_group_labels(1, axis=1))
        self.assertEqual(len(post_tuple), 2)

        post = f1[HLoc[f1.columns[0]]]
        self.assertEqual(post.__class__, Series)
        self.assertEqual(post.to_pairs(), (('x', 2), ('y', 30), ('z', 2)))

        post = f1.loc[:, HLoc[f1.columns[0]]]
        self.assertEqual(post.__class__, Series)
        self.assertEqual(post.to_pairs(), (('x', 2), ('y', 30), ('z', 2)))

        self.assertEqual(
            f1.iter_group_labels(
                1, axis=1).apply(lambda x: x.iloc[:, 0].sum()).to_pairs(),
            (('a', 34), ('b', 131)))
Esempio n. 7
0
 def test_index_level_index_types_a(self) -> None:
     idx1 = Index(('A', 'B'))
     idx2 = IndexDate.from_date_range('2019-01-05', '2019-01-08')
     idx3 = Index((1, 2))
     hidx = IndexHierarchy.from_product(idx1, idx2, idx3)
     self.assertEqual([it.__name__ for it in hidx._levels.index_types()],
                      ['Index', 'IndexDate', 'Index'])
Esempio n. 8
0
    def test_hierarchy_from_product_a(self):

        groups = Index(('A', 'B', 'C'))
        dates = IndexDate.from_date_range('2018-01-01', '2018-01-04')
        observations = Index(('x', 'y'))

        ih = IndexHierarchy.from_product(groups, dates, observations)
Esempio n. 9
0
    def test_hierarchy_extract_a(self):
        idx = IndexHierarchy.from_product(['A', 'B'], [1, 2])

        self.assertEqual(idx.iloc[1], ('A', 2))
        self.assertEqual(idx.loc[('B', 1)], ('B', 1))
        self.assertEqual(idx[2], ('B', 1))
        self.assertEqual(idx.loc[HLoc['B', 1]], ('B', 1))
Esempio n. 10
0
    def test_hierarchy_name_a(self):

        idx1 = IndexHierarchy.from_product(list('ab'), list('xy'), name='q')
        self.assertEqual(idx1.name, 'q')

        idx2 = idx1.rename('w')
        self.assertEqual(idx2.name, 'w')
Esempio n. 11
0
    def test_build_key_indexers_from_key_a(self) -> None:
        ih = IndexHierarchy.from_product(range(3), range(4, 7), tuple('ABC'))

        hlmapA = ih._map
        hlmapB = deepcopy(ih._map)
        hlmapB.encoding_can_overflow = True

        def check(
            key: tuple,  # type: ignore
            expected: tp.List[tp.List[int]],
        ) -> None:
            resultA = hlmapA.build_key_indexers(key, indices=ih._indices)
            self.assertEqual(resultA.dtype, np.uint64)
            self.assertListEqual(resultA.tolist(), expected)

            resultB = hlmapB.build_key_indexers(key, indices=ih._indices)
            self.assertEqual(resultB.dtype, object)
            self.assertListEqual(resultB.tolist(), expected)

        check((0, 5, 'A'), [0, 1, 0])  # type: ignore
        check((0, 5, ['A']), [[0, 1, 0]])
        check(([0, 1], 5, ['B']), [[0, 1, 1], [1, 1, 1]])
        check(([0, 1], 5, 'A'), [[0, 1, 0], [1, 1, 0]])
        check(
            ([0, 1], [4, 5, 6], 'C'),
            [[0, 0, 2], [0, 1, 2], [0, 2, 2], [1, 0, 2], [1, 1, 2], [1, 2, 2]])
    def test_index_hierarchy_dtypes_b(self) -> None:
        idx1 = Index(('A', 'B'), name='a')
        idx2 = IndexDate.from_date_range('2019-01-05', '2019-01-08', name='b')
        idx3 = Index((1, 2), name='c')
        hidx = IndexHierarchy.from_product(idx1, idx2, idx3)

        self.assertEqual([(x, y.kind) for x, y in hidx.dtypes.to_pairs()],
                         [('a', 'U'), ('b', 'M'), ('c', 'i')])
Esempio n. 13
0
    def test_index_init_b(self) -> None:

        idx1 = IndexHierarchy.from_product(['A', 'B'], [1, 2])

        idx2 = Index(idx1)

        self.assertEqual(idx2.values.tolist(), [('A', 1), ('A', 2), ('B', 1),
                                                ('B', 2)])
Esempio n. 14
0
    def test_hierarchy_to_pandas_a(self):

        idx1 = IndexHierarchy.from_product(list('ab'), list('xy'), name='q')

        pdidx = idx1.to_pandas()
        # NOTE: pandas .values on a hierarchical index returns an array of tuples
        self.assertEqual(idx1.values.tolist(),
                         [list(x) for x in pdidx.values.tolist()])
Esempio n. 15
0
    def test_index_hierarchy_isin_b(self) -> None:

        ih1 = IndexHierarchy.from_product((1, 2), (30, 70), (2, 5))

        with self.assertRaises(RuntimeError):
            ih1.isin([3, 4, 5])  # type: ignore # not an iterable of iterables

        post = ih1.isin(([3, 4], [2, 5, 1, 5]))
        self.assertEqual(post.sum(), 0)
    def test_hierarchy_name_a(self) -> None:

        idx1 = IndexHierarchy.from_product(list('ab'), list('xy'), name='q')
        self.assertEqual(idx1.name, 'q')

        idx2 = idx1.rename('w')
        self.assertEqual(idx2.name, 'w')
        # will provide one for each level
        self.assertEqual(idx2.names, ('w', '__index1__'))
Esempio n. 17
0
    def test_hierarchy_display_a(self):

        idx1 = IndexHierarchy.from_product(list('ab'), list('xy'), name='q')

        match = tuple(idx1.display(DisplayConfig(type_color=False)))

        self.assertEqual(match,
                         (['<IndexHierarchy: q>', ''], ['a', 'x'], ['a', 'y'],
                          ['b', 'x'], ['b', 'y'], ['<<U1>', '<<U1>']))
Esempio n. 18
0
    def test_series_to_pandas_a(self):

        s1 = Series(range(4),
                    index=IndexHierarchy.from_product(('a', 'b'), ('x', 'y')))
        df = s1.to_pandas()

        self.assertEqual(df.index.values.tolist(), [('a', 'x'), ('a', 'y'),
                                                    ('b', 'x'), ('b', 'y')])
        self.assertEqual(df.values.tolist(), [0, 1, 2, 3])
Esempio n. 19
0
    def test_hierarchy_cumprod_a(self):

        ih1 = IndexHierarchy.from_product((10, 20), (3, 7))

        # sum applies to the labels
        self.assertEqual(ih1.sum().tolist(), [60, 20])

        self.assertEqual(ih1.cumprod().tolist(),
                         [[10, 3], [100, 21], [2000, 63], [40000, 441]])
    def test_index_hierarchy_roll_a(self) -> None:

        ih1 = IndexHierarchy.from_product((1, 2), (30, 70))

        with self.assertRaises(RuntimeError):
            ih1.roll(1)  # result in invalid tree form

        self.assertEqual(
            ih1.roll(2).values.tolist(), [[2, 30], [2, 70], [1, 30], [1, 70]])
    def test_index_hierarchy_index_types_a(self) -> None:
        idx1 = Index(('A', 'B'))
        idx2 = IndexDate.from_date_range('2019-01-05', '2019-01-08')
        idx3 = Index((1, 2))
        hidx = IndexHierarchy.from_product(idx1, idx2, idx3)

        self.assertEqual([(x, y.__name__)
                          for x, y in hidx.index_types.to_pairs()],
                         [(0, 'Index'), (1, 'IndexDate'), (2, 'Index')])
    def test_index_level_dtypes_b(self) -> None:
        idx1 = Index(('A', 'B'))
        idx2 = IndexDate.from_date_range('2019-01-05', '2019-01-08')
        idx3 = Index((1, 2))

        hidx = IndexHierarchy.from_product(idx1, idx2, idx3)

        self.assertEqual([dt.kind for dt in hidx._levels.dtype_per_depth()],
                ['U', 'M', 'i'],
                )
    def test_hierarchy_to_html_datatables(self) -> None:

        ih1 = IndexHierarchy.from_product(list('ab'), list('xy'), name='q')

        with temp_file('.html', path=True) as fp:
            ih1.to_html_datatables(fp, show=False)
            with open(fp) as file:
                data = file.read()
                self.assertTrue('SFTable' in data)
                self.assertTrue(len(data) > 1000)
    def test_index_level_repr_b(self) -> None:
        """Very deep tree"""

        index = IndexHierarchy.from_product(*(range(i, i+2) for i in range(0, 20, 2)))

        msg = repr(index._levels)
        self.assertEqual(len(msg.split()), 67)

        num_ellipsis = re.findall(r"\.\.\.", msg)
        num_labels = re.findall(r"IndexLevel", msg)
        self.assertEqual(len(num_ellipsis), 8)
        self.assertEqual(len(num_labels), 15)
Esempio n. 25
0
    def test_series_reindex_b(self):
        s1 = Series(range(4),
                    index=IndexHierarchy.from_product(('a', 'b'), ('x', 'y')))
        s2 = Series(range(4),
                    index=IndexHierarchy.from_product(('b', 'c'), ('x', 'y')))

        s3 = s1.reindex(s2.index, fill_value=None)

        self.assertEqual(s3.to_pairs(),
                         ((('b', 'x'), 2), (('b', 'y'), 3), (('c', 'x'), None),
                          (('c', 'y'), None)))

        # can reindex with a different dimensionality if no matches
        self.assertEqual(
            s1.reindex((3, 4, 5, 6), fill_value=None).to_pairs(),
            ((3, None), (4, None), (5, None), (6, None)))

        self.assertEqual(
            s1.reindex((('b', 'x'), 4, 5, ('a', 'y')),
                       fill_value=None).to_pairs(),
            ((('b', 'x'), 2), (4, None), (5, None), (('a', 'y'), 1)))
Esempio n. 26
0
    def test_hierarchy_from_pandas_b(self):
        import pandas

        idx = IndexHierarchy.from_product(('I', 'II'), ('A', 'B'), (1, 2))

        self.assertEqual(list(idx.iter_label(0)), ['I', 'II'])
        self.assertEqual(list(idx.iter_label(1)), ['A', 'B', 'A', 'B'])
        self.assertEqual(list(idx.iter_label(2)), [1, 2, 1, 2, 1, 2, 1, 2])

        post = idx.iter_label(1).apply(lambda x: x.lower())
        self.assertEqual(post.to_pairs(),
                         ((0, 'a'), (1, 'b'), (2, 'a'), (3, 'b')))
    def test_index_level_repr_c(self) -> None:
        """Very wide tree"""

        index = IndexHierarchy.from_product(range(10), range(10, 20))

        msg = repr(index._levels)
        self.assertEqual(len(msg.split()), 70)

        num_ellipsis = re.findall(r"\.\.\.", msg)
        num_labels = re.findall(r"IndexLevel", msg)
        self.assertEqual(len(num_ellipsis), 1)
        self.assertEqual(len(num_labels), 5)
Esempio n. 28
0
    def test_index_hierarchy_isin_c(self) -> None:

        ih1 = IndexHierarchy.from_product((1, 2), ('a', 'b'), (2, 5))

        # multiple matches

        post1 = ih1.isin([(1, 'a', 5), (2, 'b', 2)])
        self.assertEqual(
            post1.tolist(),
            [False, True, False, False, False, False, True, False])

        post2 = ih1.isin(ih1.keys())
        self.assertEqual(post2.sum(), len(ih1))
    def test_hierarchy_rehierarch_a(self) -> None:
        ih1 = IndexHierarchy.from_product(('I', 'II'), ('B', 'A'), (2, 1))
        ih2 = ih1.rehierarch((1, 0, 2))

        self.assertEqual(
            ih2.values.tolist(),
            [['B', 'I', 2], ['B', 'I', 1], ['B', 'II', 2], ['B', 'II', 1],
             ['A', 'I', 2], ['A', 'I', 1], ['A', 'II', 2], ['A', 'II', 1]])

        ih3 = ih1.rehierarch((2, 1, 0))
        self.assertEqual(
            ih3.values.tolist(),
            [[2, 'B', 'I'], [2, 'B', 'II'], [2, 'A', 'I'], [2, 'A', 'II'],
             [1, 'B', 'I'], [1, 'B', 'II'], [1, 'A', 'I'], [1, 'A', 'II']])
Esempio n. 30
0
    def test_index_hierarchy_isin_a(self) -> None:

        ih1 = IndexHierarchy.from_product((1, 2), (30, 70), (2, 5))

        post = ih1.isin([
            (2, 30, 2),
        ])
        self.assertEqual(post.dtype, bool)
        self.assertEqual(
            post.tolist(),
            [False, False, False, False, True, False, False, False])

        extract = ih1.loc[post]
        self.assertEqual(extract.values.shape, (1, 3))