Beispiel #1
0
    def test_matmul_j(self) -> None:

        f1 = Frame.from_dict(dict(a=(1, 2, 3), b=(5, 6, 7)),
                             index=tuple('xyz'),
                             name='foo')
        a1 = np.array([[5], [0]])

        with self.assertRaises(RuntimeError):
            _ = matmul(a1, f1)
    def test_index_level_label_widths_at_depth_b(self) -> None:
        f = Frame.from_dict(
            dict(a=(1,2,3,4), b=(True, False, True, False), c=list('qrst')))
        f = f.set_index_hierarchy(['a', 'b'])

        post1 = tuple(f.index._levels.label_widths_at_depth(0))
        self.assertEqual(post1, ((1, 1), (2, 1), (3, 1), (4, 1)))
        post2 = tuple(f.index._levels.label_widths_at_depth(1))
        self.assertEqual(post2, ((True, 1), (False, 1), (True, 1), (False, 1)))
Beispiel #3
0
    def test_matmul_h(self) -> None:
        # lhs: array 2D, rhs: array 2D, Frame

        f1 = Frame.from_dict(dict(a=(1, 2, 3, 4), b=(5, 6, 7, 8)),
                             index=tuple('wxyz'))
        f2 = Frame.from_dict(dict(p=(1, 2), q=(3, 4), r=(5, 6)),
                             index=tuple('ab'))

        self.assertEqual(
            matmul(f1.values, f2).to_pairs(0),
            (('p', ((0, 11), (1, 14), (2, 17),
                    (3, 20))), ('q', ((0, 23), (1, 30), (2, 37), (3, 44))),
             ('r', ((0, 35), (1, 46), (2, 57), (3, 68)))))

        self.assertEqual(
            matmul(f1, f2.values).to_pairs(0),
            ((0, (('w', 11), ('x', 14), ('y', 17), ('z', 20))),
             (1, (('w', 23), ('x', 30), ('y', 37), ('z', 44))),
             (2, (('w', 35), ('x', 46), ('y', 57), ('z', 68)))))
Beispiel #4
0
    def test_bloc_key_normalize_a(self) -> None:
        f1 = Frame.from_dict(dict(b=(1, 2), a=(5, 6)), index=tuple('yz'))

        with self.assertRaises(RuntimeError):
            bloc_key_normalize(np.arange(4).reshape(2, 2), f1)

        post1 = bloc_key_normalize(f1['a':] >= 5, f1)  #type: ignore
        self.assertEqual(post1.tolist(), [[False, True], [False, True]])

        post2 = bloc_key_normalize(f1 < 5, f1)
        self.assertEqual(post2.tolist(), [[True, False], [True, False]])
Beispiel #5
0
    def test_display_html_pre_a(self) -> None:
        f = Frame.from_dict(dict(a=(1, 2), b=(1.2, 3.4), c=(False, True)))

        expected = f.display(
            sf.DisplayConfig(display_format='html_pre', type_color=False))

        html = '''<div style="white-space: pre; font-family: monospace">&lt;Frame&gt;
&lt;Index&gt; a       b         c      &lt;&lt;U1&gt;
&lt;Index&gt;
0       1       1.2       False
1       2       3.4       True
&lt;int64&gt; &lt;int64&gt; &lt;float64&gt; &lt;bool&gt;</div>'''

        self.assertEqual(html.strip(), str(expected).strip())
Beispiel #6
0
    def test_matmul_i(self) -> None:
        import itertools as it

        f1 = Frame.from_dict(dict(a=(1, 2), b=(5, 6)), index=tuple('yz'))

        f_container = lambda x: x
        f_values = lambda x: x.values

        for pair in ((f1, f1.T), (f1, f1.loc['y']), (f1['a'], f1),
                     (f1.loc['y'], f1.loc['z'])):
            for x, y in it.combinations(
                (f_container, f_values, f_container, f_values), 2):
                post = matmul(x(pair[0]), y(pair[1]))  # type: ignore
                if isinstance(post, (Series, Frame)):
                    self.assertTrue(post.values.tolist(),
                                    (pair[0].values @ pair[1].values).tolist())
                elif isinstance(post, np.ndarray):
                    self.assertTrue(post.tolist(),
                                    (pair[0].values @ pair[1].values).tolist())
Beispiel #7
0
    def test_key_to_ascending_key_a(self) -> None:
        self.assertEqual(key_to_ascending_key([9, 5, 1], 3), [1, 5, 9])
        self.assertEqual(
            key_to_ascending_key(np.array([9, 5, 1]), 3).tolist(),
            [1, 5, 9])  # type: ignore

        self.assertEqual(key_to_ascending_key(slice(3, 0, -1), 3),
                         slice(1, 4, 1))
        self.assertEqual(key_to_ascending_key(100, 3), 100)

        self.assertEqual(key_to_ascending_key([], 3), [])

        self.assertEqual(
            key_to_ascending_key(  # type: ignore
                Series(('a', 'b', 'c'), index=(9, 5, 1)), 3).values.tolist(),
            ['c', 'b', 'a'])

        f1 = Frame.from_dict(dict(b=(1, 2), a=(5, 6)), index=tuple('yz'))
        f2 = key_to_ascending_key(f1, f1.shape[1])
        self.assertEqual(f2.columns.values.tolist(),
                         ['a', 'b'])  # type: ignore