Ejemplo n.º 1
0
    def _extract_loc2d(self,
            row_key: GetItemKeyType = NULL_SLICE,
            column_key: GetItemKeyType = NULL_SLICE,
            ) -> tp.Union['Frame', 'Series']:
        '''
        NOTE: keys are loc keys; None is interpreted as selector, not a NULL_SLICE
        '''
        from static_frame.core.series import Series
        from static_frame.core.container_util import get_col_fill_value_factory

        fill_value = self._fill_value
        container = self._container # always a Frame

        row_key, row_is_multiple, row_is_null_slice = self._extract_key_attrs(
                row_key,
                container._index,
                )
        column_key, column_is_multiple, column_is_null_slice = self._extract_key_attrs(
                column_key,
                container._columns, #type: ignore
                )

        if row_is_multiple and column_is_multiple:
            # cannot reindex if loc keys are elements
            return container.reindex( # type: ignore
                    index=row_key if not row_is_null_slice else None,
                    columns=column_key if not column_is_null_slice else None,
                    fill_value=fill_value,
                    )
        elif not row_is_multiple and not column_is_multiple: # selecting an element
            try:
                return container.loc[row_key, column_key]
            except KeyError:
                fv = get_col_fill_value_factory(fill_value, None)(0, None)
                return fv #type: ignore
        elif not row_is_multiple:
            # row is an element, return Series indexed by columns
            if row_key in container._index: #type: ignore
                s = container.loc[row_key]
                return s.reindex(column_key, fill_value=fill_value) #type: ignore
            fv = get_col_fill_value_factory(fill_value, None)(0, None)
            return Series.from_element(fv,
                    index=column_key,
                    name=row_key,
                    )
        # columns is an element, return Series indexed by index
        if column_key in container._columns: #type: ignore
            s = container[column_key]
            return s.reindex(row_key, fill_value=fill_value) #type: ignore

        fv = get_col_fill_value_factory(fill_value, None)(0, None)
        return Series.from_element(fv,
                index=row_key,
                name=column_key,
                )
    def reload(self):
        if isinstance(self.source, Frame):
            frame = self.source
        else:
            # vd.fail(f'no support for loading {self.source.__class__}')
            raise NotImplementedError(
                f'no support for loading a Frame from {self.source}')

        # If the index is not an IndexAutoFactory, try to move it onto the Frame. If this fails it might mean we are trying to unset an auto index post selection
        if frame.index.depth > 1 or frame.index._map:  # if it is not an IndexAutoFactory
            frame = frame.unset_index()

        # VisiData assumes string column names
        if frame.columns.dtype != str:
            frame = frame.relabel(columns=frame.columns.astype(str))

        dtypes = frame.dtypes

        self.columns = []
        for col in (c for c in frame.columns if not c.startswith('__vd_')):
            self.addColumn(
                Column(
                    col,
                    type=self.dtype_to_type(dtypes[col]),
                    getter=self.getValue,
                    setter=self.setValue,
                    expr=col,
                ))

        self.rows = StaticFrameAdapter(frame)
        self._selectedMask = Series.from_element(False, index=frame.index)
 def _checkSelectedIndex(self):
     if self._selectedMask.index is not self.frame.index:
         # selection is no longer valid
         vd.status('frame.index updated, clearing {} selected rows'.format(
             self._selectedMask.sum()))
         self._selectedMask = Series.from_element(False,
                                                  index=self.frame.index)
Ejemplo n.º 4
0
 def _deferred_series(labels: tp.Iterable[str]) -> Series:
     '''
     Return an object ``Series`` of ``FrameDeferred`` objects, based on the passed in ``labels``.
     '''
     # make an object dtype
     return tp.cast(
         Series,
         Series.from_element(FrameDeferred, index=labels, dtype=object))
Ejemplo n.º 5
0
def call_attr(
        bundle: tp.Tuple[FrameOrSeries, str, tp.Any, tp.Any]) -> FrameOrSeries:
    # process pool requires a single argument
    frame, attr, args, kwargs = bundle
    func = getattr(frame, attr)
    post = func(*args, **kwargs)
    # post might be an element
    if not isinstance(post, (Frame, Series)):
        # promote to a Series to permit concatenation
        return Series.from_element(post, index=(frame.name, ))
    return post
Ejemplo n.º 6
0
def normalize_container(post: tp.Any) -> FrameOrSeries:
    # post might be an element, promote to a Series to permit concatenation
    if post.__class__ is np.ndarray:
        if post.ndim == 1:
            return Series(post)
        elif post.ndim == 2:
            return Frame(post)
        # let ndim 0 pass
    if not isinstance(post, (Frame, Series)):
        # NOTE: do not set index as (container.name,), as this can lead to diagonal formations; will already be paired with stored labels
        return Series.from_element(post, index=ELEMENT_TUPLE)
    return post
Ejemplo n.º 7
0
    def mloc(self) -> Series:
        '''Returns a Series of tuples of dtypes, one for each loaded Frame.
        '''
        if not self._loaded.any():
            return Series.from_element(None, index=self._series._index)

        def gen() -> tp.Iterator[tp.Tuple[tp.Hashable, tp.Optional[tp.Tuple[int, ...]]]]:
            for label, f in zip(self._series._index, self._series.values):
                if f is FrameDeferred:
                    yield label, None
                else:
                    yield label, tuple(f.mloc)

        return Series.from_items(gen())
Ejemplo n.º 8
0
    def mloc(self) -> Series:
        '''Returns a :obj:`Series` showing a tuple of memory locations within each loaded Frame.
        '''
        if not self._loaded.any():
            return Series.from_element(None, index=self._index)

        def gen() -> tp.Iterator[tp.Tuple[tp.Hashable, tp.Optional[tp.Tuple[int, ...]]]]:
            for label, f in zip(self._index, self._values_mutable):
                if f is FrameDeferred:
                    yield label, None
                else:
                    yield label, tuple(f.mloc)

        return Series.from_items(gen())
Ejemplo n.º 9
0
 def _deferred_series(
         labels: tp.Iterable[tp.Hashable],
         *,
         index_constructor: IndexConstructor = None,
         ) -> Series:
     '''
     Return an object ``Series`` of ``FrameDeferred`` objects, based on the passed in ``labels``.
     '''
     # NOTE: need to accept an  IndexConstructor to support reanimating Index subtypes, IH
     return Series.from_element(FrameDeferred,
             index=labels,
             dtype=DTYPE_OBJECT,
             index_constructor=index_constructor,
             )
 def clearSelected(self):
     self._selectedMask = Series.from_element(False, index=self.frame.index)