예제 #1
0
    def test_display_display_columns_a(self):

        config_columns_8 = sf.DisplayConfig.from_default(display_columns=8,
                                                         type_color=False)
        config_columns_5 = sf.DisplayConfig.from_default(display_columns=5,
                                                         type_color=False)

        columns = list(''.join(x)
                       for x in combinations(string.ascii_lowercase, 2))
        f = FrameGO(index=range(4))
        for i, col in enumerate(columns):
            f[col] = Series(i, index=range(4))

        self.assertEqual(
            f.display(config_columns_8).to_rows(), [
                '<FrameGO>',
                '<IndexGO> ab      ac      ... xz      yz      <<U2>',
                '<Index>                   ...',
                '0         0       1       ... 323     324',
                '1         0       1       ... 323     324',
                '2         0       1       ... 323     324',
                '3         0       1       ... 323     324',
                '<int64>   <int64> <int64> ... <int64> <int64>'
            ])

        self.assertEqual(
            f.display(config_columns_5).to_rows(), [
                '<FrameGO>', '<IndexGO> ab      ... yz      <<U2>',
                '<Index>           ...', '0         0       ... 324',
                '1         0       ... 324', '2         0       ... 324',
                '3         0       ... 324', '<int64>   <int64> ... <int64>'
            ])
예제 #2
0
    def test_display_cell_fill_width_a(self) -> None:

        config_width_12 = sf.DisplayConfig.from_default(
            cell_max_width=12, cell_max_width_leftmost=12, type_color=False)
        config_width_6 = sf.DisplayConfig.from_default(
            cell_max_width=6, cell_max_width_leftmost=6, type_color=False)

        def chunks(size: int, count: int) -> tp.Iterator[str]:
            pos = 0
            for _ in range(count):
                yield LONG_SAMPLE_STR[pos:pos + size]
                pos = pos + size

        s = Series(chunks(20, 3), index=('a', 'b', 'c'))

        self.assertEqual(
            s.display(config=config_width_12).to_rows(), [
                '<Series>', '<Index>', 'a        Lorem ips...',
                'b        t amet, c...', 'c        adipiscin...',
                '<<U1>    <<U20>'
            ])

        self.assertEqual(
            s.display(config=config_width_6).to_rows(), [
                '<Se...', '<In...', 'a      Lor...', 'b      t a...',
                'c      adi...', '<<U1>  <<U20>'
            ])

        config = sf.DisplayConfig.from_default(type_color=False,
                                               cell_max_width_leftmost=20)

        row_count = 2
        index = [str(chr(x)) for x in range(97, 97 + row_count)]
        f = FrameGO(index=index)

        for i in range(4):
            chunker = iter(chunks(10, row_count))
            s = Series((x for x in chunker), index=index)
            f[i] = s

        f.columns._update_array_cache()

        self.assertEqual(
            f.display(config=config).to_rows(), [
                '<FrameGO>',
                '<IndexGO> 0          1          2          3          <int64>',
                '<Index>',
                'a         Lorem ipsu Lorem ipsu Lorem ipsu Lorem ipsu',
                'b         m dolor si m dolor si m dolor si m dolor si',
                '<<U1>     <<U10>     <<U10>     <<U10>     <<U10>'
            ])

        self.assertEqual(
            f.display(config=config_width_6).to_rows(), [
                '<Fr...', '<In... 0      1      2      3      <in...',
                '<In...', 'a      Lor... Lor... Lor... Lor...',
                'b      m d... m d... m d... m d...',
                '<<U1>  <<U10> <<U10> <<U10> <<U10>'
            ])
    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)))))
예제 #4
0
    def test_index_date_threshold_a(self) -> None:

        index = IndexDate.from_date_range('2019-01-01', '2020-02-28')
        threshold_day = 15
        threshold_month = 2
        lag = 2

        f = FrameGO(index=index)
        f['month_day'] = f.index.via_dt.month * 100 + f.index.via_dt.day
        f['transition'] = f['month_day'] >= (threshold_month * 100 +
                                             threshold_day)
        f['year_shift'] = Series.from_element(
            lag, index=index).assign[f['transition']](lag - 1)
        f['year'] = f.index.via_dt.year - f['year_shift'].values

        self.assertEqual(f['transition'].sum(), 334)
        self.assertEqual(f['year'].values.tolist(), [
            2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
            2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
            2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
            2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
            2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
            2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
            2019, 2019, 2019, 2019, 2019, 2019
        ])
    def test_frame_iter_tuple_items_a(self) -> None:
        records = ((1, 2, 'a', False, True), (30, 50, 'b', True, False))

        f1 = FrameGO.from_records(records,
                                  columns=('p', 'q', 'r', 's', 't'),
                                  index=('x', 'y'))

        post1 = list(f1.iter_tuple_items(constructor=list))
        self.assertEqual(post1, [('p', [1, 30]), ('q', [2, 50]),
                                 ('r', ['a', 'b']), ('s', [False, True]),
                                 ('t', [True, False])])
예제 #6
0
    def test_frame_via_fill_value_e(self) -> None:

        f1 = FrameGO(index=range(5))
        f1.via_fill_value(0)['a'] = Series([10, 20], index=(2, 4))
        f1.via_fill_value(-1)['b'] = Series([10, 20], index=(0, 1))

        self.assertEqual(f1.to_pairs(),
                         (('a', ((0, 0), (1, 0), (2, 10), (3, 0), (4, 20))),
                          ('b', ((0, 10), (1, 20), (2, -1), (3, -1),
                                 (4, -1)))))

        f2 = Frame(index=range(5))
        with self.assertRaises(TypeError):
            f2.via_fill_value(0)['a'] = range(5)  # type: ignore #pylint: disable=E1137
    def test_frame_iter_tuple_e(self) -> None:
        records = ((1, 2, 'a', False, True), (30, 50, 'b', True, False))

        f1 = FrameGO.from_records(records,
                                  columns=('p', 'q', 'r', 's', 't'),
                                  index=('x', 'y'))

        class Record(tp.NamedTuple):
            x: object
            y: object

        post1 = list(f1.iter_tuple(constructor=Record))
        self.assertTrue(all(isinstance(x, Record) for x in post1))

        post2 = list(f1.iter_tuple(constructor=tuple))
        self.assertEqual(post2, [(1, 30), (2, 50), ('a', 'b'), (False, True),
                                 (True, False)])