Ejemplo n.º 1
0
    def wrap(self, a, index=None, columns=None, dtype=None, group_by=None):
        """Wrap a NumPy array using the stored metadata."""
        checks.assert_ndim(a, (1, 2))
        _self = self.resolve(group_by=group_by)

        a = np.asarray(a)
        a = reshape_fns.soft_to_ndim(a, self.ndim)
        if index is None:
            index = _self.index
        if columns is None:
            columns = _self.columns
        if columns is not None and len(columns) == 1:
            name = columns[0]
            if name == 0:  # was a Series before
                name = None
        else:
            name = None
        if index is not None:
            checks.assert_shape_equal(a, index, axis=(0, 0))
        if a.ndim == 2 and columns is not None:
            checks.assert_shape_equal(a, columns, axis=(1, 0))
        if a.ndim == 1:
            return pd.Series(a, index=index, name=name, dtype=dtype)
        if a.ndim == 2:
            if a.shape[1] == 1 and _self.ndim == 1:
                return pd.Series(a[:, 0], index=index, name=name, dtype=dtype)
            return pd.DataFrame(a, index=index, columns=columns, dtype=dtype)
        raise ValueError(f"{a.ndim}-d input is not supported")
Ejemplo n.º 2
0
    def wrap(self, a, index=None, columns=None, ndim=None, dtype=None):
        """Wrap a NumPy array using the stored metadata."""
        checks.assert_ndim(a, (1, 2))

        a = np.asarray(a)
        if ndim is None:
            ndim = self.ndim
        if ndim is not None:
            a = reshape_fns.soft_to_ndim(a, self.ndim)
        if index is None:
            index = self.index
        if columns is None:
            columns = self.columns
        if columns is not None and len(columns) == 1:
            name = columns[0]
            if name == 0:  # was a Series before
                name = None
        else:
            name = None

        # Perform checks
        if index is not None:
            checks.assert_same_shape(a, index, axis=(0, 0))
        if a.ndim == 2 and columns is not None:
            checks.assert_same_shape(a, columns, axis=(1, 0))

        if a.ndim == 1:
            return pd.Series(a, index=index, name=name, dtype=dtype)
        return pd.DataFrame(a, index=index, columns=columns, dtype=dtype)
Ejemplo n.º 3
0
        def _wrap_reduced(arr):
            nonlocal name_or_index

            arr = np.asarray(arr)
            if fillna is not None:
                arr[pd.isnull(arr)] = fillna
            if arr.ndim == 0:
                # Scalar per Series/DataFrame
                return pd.Series(arr, dtype=dtype)[0]
            if arr.ndim == 1:
                if _self.ndim == 1:
                    if arr.shape[0] == 1:
                        # Scalar per Series/DataFrame with one column
                        return pd.Series(arr, dtype=dtype)[0]
                    # Array per Series
                    sr_name = columns[0]
                    if sr_name == 0:  # was arr Series before
                        sr_name = None
                    if isinstance(name_or_index, str):
                        name_or_index = None
                    return pd.Series(arr,
                                     index=name_or_index,
                                     name=sr_name,
                                     dtype=dtype)
                # Scalar per column in arr DataFrame
                return pd.Series(arr,
                                 index=columns,
                                 name=name_or_index,
                                 dtype=dtype)
            if arr.ndim == 2:
                if arr.shape[1] == 1 and _self.ndim == 1:
                    arr = reshape_fns.soft_to_ndim(arr, 1)
                    # Array per Series
                    sr_name = columns[0]
                    if sr_name == 0:  # was arr Series before
                        sr_name = None
                    if isinstance(name_or_index, str):
                        name_or_index = None
                    return pd.Series(arr,
                                     index=name_or_index,
                                     name=sr_name,
                                     dtype=dtype)
                # Array per column in DataFrame
                if isinstance(name_or_index, str):
                    name_or_index = None
                return pd.DataFrame(arr,
                                    index=name_or_index,
                                    columns=columns,
                                    dtype=dtype)
            raise ValueError(f"{arr.ndim}-d input is not supported")
Ejemplo n.º 4
0
    def wrap(self,
             a: tp.ArrayLike,
             index: tp.Optional[tp.IndexLike] = None,
             columns: tp.Optional[tp.IndexLike] = None,
             dtype: tp.Optional[tp.PandasDTypeLike] = None,
             group_by: tp.GroupByLike = None) -> tp.SeriesFrame:
        """Wrap a NumPy array using the stored metadata."""
        checks.assert_ndim(a, (1, 2))
        _self = self.resolve(group_by=group_by)

        if index is None:
            index = _self.index
        if not isinstance(index, pd.Index):
            index = pd.Index(index)
        if columns is None:
            columns = _self.columns
        if not isinstance(columns, pd.Index):
            columns = pd.Index(columns)
        if len(columns) == 1:
            name = columns[0]
            if name == 0:  # was a Series before
                name = None
        else:
            name = None

        arr = np.asarray(a)
        arr = reshape_fns.soft_to_ndim(arr, self.ndim)
        checks.assert_shape_equal(arr, index, axis=(0, 0))
        if arr.ndim == 2:
            checks.assert_shape_equal(arr, columns, axis=(1, 0))
        if arr.ndim == 1:
            return pd.Series(arr, index=index, name=name, dtype=dtype)
        if arr.ndim == 2:
            if arr.shape[1] == 1 and _self.ndim == 1:
                return pd.Series(arr[:, 0],
                                 index=index,
                                 name=name,
                                 dtype=dtype)
            return pd.DataFrame(arr, index=index, columns=columns, dtype=dtype)
        raise ValueError(f"{arr.ndim}-d input is not supported")
Ejemplo n.º 5
0
 def _wrap(arr):
     arr = np.asarray(arr)
     checks.assert_ndim(arr, (1, 2))
     if fillna is not None:
         arr[pd.isnull(arr)] = fillna
     arr = reshape_fns.soft_to_ndim(arr, self.ndim)
     checks.assert_shape_equal(arr, index, axis=(0, 0))
     if arr.ndim == 2:
         checks.assert_shape_equal(arr, columns, axis=(1, 0))
     if arr.ndim == 1:
         return pd.Series(arr, index=index, name=name, dtype=dtype)
     if arr.ndim == 2:
         if arr.shape[1] == 1 and _self.ndim == 1:
             return pd.Series(arr[:, 0],
                              index=index,
                              name=name,
                              dtype=dtype)
         return pd.DataFrame(arr,
                             index=index,
                             columns=columns,
                             dtype=dtype)
     raise ValueError(f"{arr.ndim}-d input is not supported")
Ejemplo n.º 6
0
    def wrap(self,
             a,
             index=None,
             columns=None,
             dtype=None,
             collapse=None,
             **kwargs):
        """Wrap a NumPy array using the stored metadata."""
        checks.assert_ndim(a, (1, 2))
        group_by = self.grouper.resolve_group_by(**kwargs)

        a = np.asarray(a)
        a = reshape_fns.soft_to_ndim(a, self.ndim)
        if index is None:
            index = self.index
        if columns is None:
            columns = self.grouper.get_columns(**kwargs)
        if collapse is None:
            collapse = group_by is not None and group_by is not False and self.grouped_ndim == 1
        if columns is not None and len(columns) == 1:
            name = columns[0]
            if name == 0:  # was a Series before
                name = None
        else:
            name = None

        # Perform checks
        if index is not None:
            checks.assert_shape_equal(a, index, axis=(0, 0))
        if a.ndim == 2 and columns is not None:
            checks.assert_shape_equal(a, columns, axis=(1, 0))

        if a.ndim == 1:
            return pd.Series(a, index=index, name=name, dtype=dtype)
        if a.ndim == 2 and a.shape[1] == 1 and collapse:
            return pd.Series(a[:, 0], index=index, name=name, dtype=dtype)
        return pd.DataFrame(a, index=index, columns=columns, dtype=dtype)