def test_index_level_extend_b(self) -> None:
     level0 = IndexLevelGO(index=IndexGO(('a', 'b')), targets=None)
     level1 = IndexLevelGO(index=IndexGO(('c', 'd')), targets=None)
     # RuntimeError: found IndexLevel with None as targets
     with self.assertRaises(RuntimeError):
         level0.extend(level1)
Esempio n. 2
0
 def test_index_binary_operators_d(self) -> None:
     idx = IndexGO((20, 30, 40))
     idx.extend((50, 60))
     a1 = idx * 2
     self.assertEqual(a1.tolist(), [40, 60, 80, 100, 120])
Esempio n. 3
0
    def test_index_go_c(self) -> None:

        index = IndexGO(('a', (2, 5), 'c'))
        with self.assertRaises(KeyError):
            index.append((2, 5))
Esempio n. 4
0
 def test_index_nbytes_a(self) -> None:
     idx = IndexGO(('a', 'b', 'c', 'd'))
     self.assertEqual(idx.nbytes, 16)
     idx.append('e')
     self.assertEqual(idx.nbytes, 20)
Esempio n. 5
0
    def test_index_positions_a(self) -> None:
        idx1 = IndexGO(('a', 'b', 'c', 'd'), name='foo')
        self.assertEqual(idx1.positions.tolist(), list(range(4)))

        idx1.append('e')
        self.assertEqual(idx1.positions.tolist(), list(range(5)))
Esempio n. 6
0
 def test_index_dtype_a(self) -> None:
     idx = IndexGO(('a', 'b', 'c', 'd'))
     self.assertEqual(str(idx.dtype), '<U1')
     idx.append('eee')
     self.assertEqual(str(idx.dtype), '<U3')
Esempio n. 7
0
 def test_index_ndim_a(self) -> None:
     idx = IndexGO(('a', 'b', 'c', 'd'))
     self.assertEqual(idx.ndim, 1)
     idx.append('e')
     self.assertEqual(idx.ndim, 1)
Esempio n. 8
0
    def test_index_to_html_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c'))
        self.assertEqual(idx1.to_html(),
                '<table border="1"><thead></thead><tbody><tr><td>a</td></tr><tr><td>b</td></tr><tr><td>c</td></tr></tbody></table>')
Esempio n. 9
0
    def test_index_label_widths_at_depth_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c', 'd', 'e'))
        self.assertEqual(tuple(idx1.label_widths_at_depth(0)),
                         (('a', 1), ('b', 1), ('c', 1), ('d', 1), ('e', 1)))
Esempio n. 10
0
    def test_index_reversed_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c'), name='foo')
        idx1.append('d')
        self.assertEqual(list(reversed(idx1)), ['d', 'c', 'b', 'a'])
Esempio n. 11
0
    def test_index_to_pandas_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c', 'd'), name='foo')
        pdidx = idx1.to_pandas()
        self.assertEqual(pdidx.name, idx1.name)
        self.assertTrue((pdidx.values == idx1.values).all())
Esempio n. 12
0
    def test_index_iter_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c'), name='foo')
        idx1.append('d')
        self.assertEqual(list(idx1), ['a', 'b', 'c', 'd'])
Esempio n. 13
0
    def test_index_extract_iloc_a(self) -> None:

        idx = IndexGO(('a', 'b', 'c', 'd'))
        idx.append('e')
        post = idx._extract_iloc(slice(None))
        self.assertEqual(post.values.tolist(), ['a', 'b', 'c', 'd', 'e'])
Esempio n. 14
0
    def from_pandas(cls,
            value: 'pandas.Index',
            ) -> 'IndexBase':
        '''
        Given a Pandas index, return the appropriate IndexBase derived class.
        '''
        import pandas
        if not isinstance(value, pandas.Index):
            raise ErrorInitIndex(f'from_pandas must be called with a Pandas Index object, not: {type(value)}')

        from static_frame import Index
        from static_frame import IndexGO
        from static_frame import IndexHierarchy
        from static_frame import IndexHierarchyGO
        from static_frame import IndexNanosecond
        from static_frame import IndexNanosecondGO
        from static_frame.core.index_datetime import IndexDatetime

        if isinstance(value, pandas.MultiIndex):
            if value.has_duplicates:
                raise ErrorInitIndex(f'cannot create IndexHierarchy from a MultiIndex with duplicates: {value}')

            # iterating over a hierarchical index will iterate over labels
            name: tp.Optional[tp.Tuple[tp.Hashable, ...]] = tuple(value.names)

            # if not assigned Pandas returns None for all components, which will raise issue if trying to unset this index.
            if all(n is None for n in name): #type: ignore
                name = None

            hierarchy_constructor = IndexHierarchy if cls.STATIC else IndexHierarchyGO

            def build_index(pd_idx: pandas.Index) -> Index:
                # NOTE: Newer versions of pandas will not require Python date objects to live inside
                # a DatetimeIndex. Instead, it will be a regular Index with dtype=object.
                # Only numpy datetime objects are put into a DatetimeIndex.
                if isinstance(pd_idx, pandas.DatetimeIndex):
                    constructor: tp.Type[Index] = IndexNanosecond
                else:
                    constructor = Index

                if cls.STATIC:
                    return constructor(pd_idx, name=pd_idx.name)
                return tp.cast(Index, constructor._MUTABLE_CONSTRUCTOR(pd_idx))

            indices: tp.List[Index] = []
            indexers: np.ndarray = np.empty((value.nlevels, len(value)), dtype=DTYPE_INT_DEFAULT)

            for i, (levels, codes) in enumerate(zip(value.levels, value.codes)):
                indexers[i] = codes
                indices.append(build_index(levels))

            indexers.flags.writeable = False

            return hierarchy_constructor(
                    indices=indices,
                    indexers=indexers,
                    name=name,
                    )

        elif isinstance(value, pandas.DatetimeIndex):
            # if IndexDatetime, use cls, else use IndexNanosecond
            if issubclass(cls, IndexDatetime):
                return cls(value, name=value.name)
            else:
                if not cls.STATIC:
                    return IndexNanosecondGO(value, name=value.name)
                return IndexNanosecond(value, name=value.name)

        if not cls.STATIC:
            return IndexGO(value, name=value.name)
        return Index(value, name=value.name)
Esempio n. 15
0
 def test_index_equals_f(self) -> None:
     a = IndexGO([1, 2, 3])
     b = IndexGO([1, 2, 3])
     b.append(4)
     self.assertFalse(a.equals(b))
Esempio n. 16
0
 def test_index_label_widths_at_depth_b(self) -> None:
     idx1 = IndexGO(('a', 'b', 'c', 'd', 'e'))
     with self.assertRaises(RuntimeError):
         next(idx1.label_widths_at_depth(1))
Esempio n. 17
0
 def test_index_mloc_b(self) -> None:
     idx = IndexGO(('a', 'b', 'c', 'd'))
     idx.append('e')
     self.assertTrue(idx.mloc == idx[:2].mloc)  #type: ignore
Esempio n. 18
0
    def test_index_bool_b(self) -> None:

        idx1 = IndexGO(())
        self.assertFalse(bool(idx1))
        idx1.append('a')
        self.assertTrue(bool(idx1))
Esempio n. 19
0
 def test_index_shape_a(self) -> None:
     idx = IndexGO(('a', 'b', 'c', 'd'))
     self.assertEqual(idx.shape, (4, ))
     idx.append('e')
     self.assertEqual(idx.shape, (5, ))
Esempio n. 20
0
    def test_index_values_at_depth_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c'))
        self.assertEqual(idx1.values_at_depth(0).tolist(), ['a', 'b', 'c'])
        with self.assertRaises(RuntimeError):
            idx1.values_at_depth(1)
Esempio n. 21
0
 def test_index_size_a(self) -> None:
     idx = IndexGO(('a', 'b', 'c', 'd'))
     self.assertEqual(idx.size, 4)
     idx.append('e')
     self.assertEqual(idx.size, 5)
Esempio n. 22
0
    def test_index_head_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c', 'd', 'e'))
        self.assertEqual(idx1.head(2).values.tolist(), ['a', 'b'])
Esempio n. 23
0
 def test_index_rename_a(self) -> None:
     idx1 = IndexGO(('a', 'b', 'c', 'd'), name='foo')
     idx1.append('e')
     idx2 = idx1.rename('bar')
     self.assertEqual(idx2.name, 'bar')
Esempio n. 24
0
    def test_index_tail_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c', 'd', 'e'))
        self.assertEqual(idx1.tail(2).values.tolist(), ['d', 'e'])
Esempio n. 25
0
 def test_index_unary_operators_b(self) -> None:
     idx = IndexGO((20, 30, 40))
     idx.append(50)
     a1 = -idx
     self.assertEqual(a1.tolist(), [-20, -30, -40, -50])
Esempio n. 26
0
    def test_index_via_str_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c', 'd', 'e'))
        a1 = idx1.via_str.upper()

        self.assertEqual(a1.tolist(), ['A', 'B', 'C', 'D', 'E'])
Esempio n. 27
0
    def test_index_ufunc_axis_b(self) -> None:

        idx = IndexGO((30, 40, 20))
        idx.append(10)
        self.assertEqual(idx.sum(), 100)
Esempio n. 28
0
    def test_index_equals_a(self) -> None:

        idx1 = IndexGO(('a', 'b', 'c', 'd', 'e'))
        idx2 = Index(('a', 'b', 'c', 'd', 'e'))
        idx3 = IndexGO(('a', 'b', 'c', 'd', 'e'), name='foo')
        idx4 = IndexGO(('a', 'b', 'c', 'd', 'e'))
        idx5 = IndexGO(('a', 'b', 'c', 'd'))

        self.assertEqual(idx1.equals(3), False)
        self.assertEqual(idx1.equals(False), False)
        self.assertEqual(idx1.equals([3, 4, 5]), False)

        self.assertEqual(idx1.equals(idx2, compare_class=True), False)
        self.assertEqual(idx1.equals(idx2, compare_class=False), True)

        self.assertEqual(idx1.equals(idx3, compare_name=True), False)
        self.assertEqual(idx1.equals(idx3, compare_name=False), True)
        self.assertEqual(idx1.equals(idx5), False)

        self.assertEqual(idx1.equals(idx1), True)
        self.assertEqual(idx1.equals(idx4), True)
Esempio n. 29
0
    def test_index_drop_iloc_a(self) -> None:

        idx = IndexGO(('a', 'b', 'c', 'd'))
        idx.append('e')
        post = idx._drop_iloc([1, 2])
        self.assertEqual(post.values.tolist(), ['a', 'd', 'e'])
Esempio n. 30
0
 def test_index_level_extend_a(self) -> None:
     level0 = IndexLevelGO(index=IndexGO(('a', 'b')), targets=None)
     with self.assertRaises(RuntimeError):
         level0.extend(level0)