Exemplo n.º 1
0
    def __init__(
        self,
        series: tp.Union[Series, tp.Iterable[Bus]],
        *,
        index: tp.Optional[tp.Union[IndexBase, IndexAutoFactoryType]] = None,
        index_constructor: tp.Optional[IndexConstructor] = None,
        deepcopy_from_bus: bool = False,
        hierarchy: tp.Optional[IndexHierarchy] = None,
        own_index: bool = False,
    ) -> None:
        '''
        Args:
            series: An iterable (or :obj:`Series`) of :obj:`Bus`. The length of this container is not the same as ``index``, if provided.
            index: Optionally provide an index for the :obj:`Frame` contained in all :obj:`Bus`.
            index_constructor:
            deepcopy_from_bus:
            hierarchy:
            own_index:
        '''

        if isinstance(series, Series):
            if series.dtype != DTYPE_OBJECT:
                raise ErrorInitYarn(
                    f'Series passed to initializer must have dtype object, not {series.dtype}'
                )
            self._series = series  # Bus by Bus label
        else:
            self._series = Series(series,
                                  dtype=DTYPE_OBJECT)  # get a default index

        self._deepcopy_from_bus = deepcopy_from_bus

        # _hierarchy might be None while we still need to set self._index
        if hierarchy is None:
            self._hierarchy = buses_to_hierarchy(
                self._series.values,
                self._series.index,
                deepcopy_from_bus=self._deepcopy_from_bus,
                init_exception_cls=ErrorInitYarn,
            )
        else:
            self._hierarchy = hierarchy

        if own_index:
            self._index = index  #type: ignore
        elif index is None or index is IndexAutoFactory:
            self._index = IndexAutoFactory.from_optional_constructor(
                len(self._hierarchy),
                default_constructor=Index,
                explicit_constructor=index_constructor)
        else:  # an iterable of labels or an Index
            self._index = index_from_optional_constructor(
                index,  #type: ignore
                default_constructor=Index,
                explicit_constructor=index_constructor)

        if len(self._index) != len(self._hierarchy):
            raise ErrorInitYarn(
                f'Length of supplied index ({len(self._index)}) not of sufficient size ({len(self._hierarchy)}).'
            )
    def test_index_auto_factory_a(self) -> None:

        idx1 = IndexAutoFactory.from_optional_constructor(
            4, default_constructor=Index)
        self.assertEqual(idx1._map is None, True)  #type: ignore
        self.assertEqual(len(idx1), 4)
        self.assertEqual(idx1.STATIC, True)
Exemplo n.º 3
0
    def test_index_auto_factory_a(self) -> None:

        idx1 = IndexAutoFactory.from_optional_constructor(
            4, default_constructor=Index)
        self.assertEqual(idx1._loc_is_iloc, True)
        self.assertEqual(len(idx1), 4)
        self.assertEqual(idx1.STATIC, True)
    def test_index_auto_factory_c(self) -> None:

        idx1 = IndexAutoFactory.from_optional_constructor(
            5, default_constructor=IndexGO, explicit_constructor=Index)
        # when using an alternate constructor, loc_is_iloc will not be set
        self.assertEqual(idx1._map is None, False)  #type: ignore
        self.assertEqual(len(idx1), 5)
        self.assertEqual(idx1.STATIC, True)
    def test_index_auto_factory_b(self) -> None:

        idx1 = IndexAutoFactory.from_optional_constructor(
            8, default_constructor=IndexGO)
        self.assertEqual(idx1._map is None, True)  #type: ignore
        self.assertEqual(len(idx1), 8)
        self.assertEqual(idx1.STATIC, False)

        # go funcitonality
        assert isinstance(idx1, IndexGO)
        idx1.append(8)
        self.assertEqual(idx1._map is None, True)
        self.assertEqual(len(idx1), 9)
 def test_index_auto_factory_from_optional_constructor(self) -> None:
     initializer = 3
     explicit_constructor = IndexDefaultFactory(name='foo')
     default_constructor = IndexDate
     post = IndexAutoFactory.from_optional_constructor(
         initializer=initializer,
         explicit_constructor=explicit_constructor,
         default_constructor=default_constructor,
     )
     self.assertEqual(post.name, 'foo')
     self.assertEqual(post.values.tolist(), [
         datetime.date(1970, 1, 1),
         datetime.date(1970, 1, 2),
         datetime.date(1970, 1, 3)
     ])