Example #1
0
    def test_store_xlsx_read_many_f(self) -> None:
        records = (
            (2, 2, 'a', False, None),
            (30, 73, 'd', True, None),
            (None, None, None, None, None),
            (None, None, None, None, None),
        )
        f1 = Frame.from_records(records, columns=('p', 'q', 'r', 's', 't'))

        with temp_file('.xlsx') as fp:
            f1.to_xlsx(fp,
                       label='f1',
                       include_index=False,
                       include_columns=False)

            st1 = StoreXLSX(fp)
            c = StoreConfig(
                index_depth=3,  # force coverage
                columns_depth=0,
                trim_nadir=True,
            )
            f2 = next(st1.read_many(('f1', ), config=c))
            self.assertEqual(f2.shape, (2, 1))
            self.assertEqual(f2.to_pairs(), ((0, (((2, 2, 'a'), False),
                                                  ((30, 73, 'd'), True))), ))
    def test_store_xlsx_read_many_d(self) -> None:
        records = (
                (2, 2, 'a', False, None),
                (30, 73, 'd', True, None),
                (None, None, None, None, None),
                (None, None, None, None, None),
                )
        columns = IndexHierarchy.from_labels((
                ('a', 1), ('a', 2), ('b', 1), ('b', 2), (None, None)
                ))
        f1 = Frame.from_records(records, columns=columns)

        with temp_file('.xlsx') as fp:
            f1.to_xlsx(fp, label='f1', include_index=False, include_columns=True)

            st1 = StoreXLSX(fp)
            c = StoreConfig(
                    index_depth=0,
                    columns_depth=2,
                    trim_nadir=True,
                    )
            f2 = next(st1.read_many(('f1',), config=c))
            self.assertEqual(f2.shape, (2, 4))
            self.assertEqual(f2.to_pairs(),
                    ((('a', 1), ((0, 2), (1, 30))), (('a', 2), ((0, 2), (1, 73))), (('b', 1), ((0, 'a'), (1, 'd'))), (('b', 2), ((0, False), (1, True)))))
Example #3
0
    def test_store_xlsx_read_many_a(self) -> None:

        f1 = Frame.from_dict(dict(x=(1, 2, -5, 200), y=(3, 4, -5, -3000)),
                             index=IndexHierarchy.from_product(('I', 'II'),
                                                               ('a', 'b')),
                             name='f1')
        f2 = Frame.from_dict(dict(a=(1, 2, 3), b=(4, 5, 6)),
                             index=('x', 'y', 'z'),
                             name='f2')
        f3 = Frame.from_records(((10, 20, 50, 60), (50.0, 60.4, -50, -60)),
                                index=('p', 'q'),
                                columns=IndexHierarchy.from_product(
                                    ('I', 'II'), ('a', 'b')),
                                name='f3')
        f4 = Frame.from_records(
            (
                (10, 20, 50, False, 10, 20, 50, False),
                (50.0, 60.4, -50, True, 50.0, 60.4, -50, True),
                (234, 44452, 0, False, 234, 44452, 0, False),
                (4, -4, 2000, True, 4, -4, 2000, True),
                (10, 20, 50, False, 10, 20, 50, False),
                (50.0, 60.4, -50, True, 50.0, 60.4, -50, True),
                (234, 44452, 0, False, 234, 44452, 0, False),
                (4, -4, 2000, True, 4, -4, 2000, True),
            ),
            index=IndexHierarchy.from_product(
                ('top', 'bottom'), ('far', 'near'), ('left', 'right')),
            columns=IndexHierarchy.from_product(('I', 'II'), ('a', 'b'),
                                                (1, 2)),
            name='f4')

        frames = (f1, f2, f3, f4)
        config_map_write = StoreConfigMap.from_config(
            StoreConfig(include_index=True, include_columns=True))

        with temp_file('.xlsx') as fp:

            st1 = StoreXLSX(fp)
            st1.write(((f.name, f) for f in frames), config=config_map_write)

            sheet_names = tuple(
                st1.labels())  # this will read from file, not in memory
            self.assertEqual(tuple(f.name for f in frames), sheet_names)

            config_map_read: tp.Dict[tp.Hashable, StoreConfig] = {}
            for i, name in enumerate(sheet_names):
                f_src = frames[i]
                c = StoreConfig(index_depth=f_src.index.depth,
                                columns_depth=f_src.columns.depth)
                config_map_read[name] = c

            for i, f_loaded in enumerate(
                    st1.read_many(sheet_names, config=config_map_read)):
                f_src = frames[i]
                self.assertEqualFrames(f_src, f_loaded, compare_dtype=False)
    def test_store_xlsx_read_many_e(self) -> None:
        records = (
                (2, 2, 'a', False, None),
                (30, 73, 'd', True, None),
                (None, None, None, None, None),
                (None, None, None, None, None),
                )
        f1 = Frame.from_records(records,
                columns=('p', 'q', 'r', 's', None))

        with temp_file('.xlsx') as fp:
            f1.to_xlsx(fp, label='f1', include_index=False, include_columns=True)

            st1 = StoreXLSX(fp)
            c = StoreConfig(
                    index_depth=0,
                    columns_depth=1,
                    trim_nadir=True,
                    )
            # NOTE: if store_filter is None, None is not properly identified as a nadir-area entity and trim_nadir does not drop any rows or columns here
            f2 = next(st1.read_many(('f1',), store_filter=None, config=c))
            self.assertEqual(f2.shape, (4, 5))