Пример #1
0
    def reduce(self,
               reduce_func_nb,
               *args,
               idx_arr=None,
               to_array=False,
               to_idx=False,
               idx_labeled=True,
               default_val=np.nan,
               group_by=None,
               **kwargs):
        """Reduce mapped array by column.

        If `to_array` is False and `to_idx` is False, see `vectorbt.records.nb.reduce_mapped_nb`.
        If `to_array` is False and `to_idx` is True, see `vectorbt.records.nb.reduce_mapped_to_idx_nb`.
        If `to_array` is True and `to_idx` is False, see `vectorbt.records.nb.reduce_mapped_to_array_nb`.
        If `to_array` is True and `to_idx` is True, see `vectorbt.records.nb.reduce_mapped_to_idx_array_nb`.

        If `to_idx` is True, must pass `idx_arr`. Set `idx_labeled` to False to return raw positions instead
        of labels. Use `default_val` to set the default value. Set `group_by` to False to disable grouping.

        `**kwargs` will be passed to `vectorbt.base.array_wrapper.ArrayWrapper.wrap_reduced`."""
        # Perform checks
        checks.assert_numba_func(reduce_func_nb)
        if idx_arr is None:
            if self.idx_arr is None:
                if to_idx:
                    raise ValueError("Must pass idx_arr")
            idx_arr = self.idx_arr

        # Perform main computation
        col_map = self.col_mapper.get_col_map(group_by=group_by)
        if not to_array:
            if not to_idx:
                out = nb.reduce_mapped_nb(self.values, col_map, default_val,
                                          reduce_func_nb, *args)
            else:
                out = nb.reduce_mapped_to_idx_nb(self.values, col_map, idx_arr,
                                                 default_val, reduce_func_nb,
                                                 *args)
        else:
            if not to_idx:
                out = nb.reduce_mapped_to_array_nb(self.values, col_map,
                                                   default_val, reduce_func_nb,
                                                   *args)
            else:
                out = nb.reduce_mapped_to_idx_array_nb(self.values, col_map,
                                                       idx_arr, default_val,
                                                       reduce_func_nb, *args)

        # Perform post-processing
        if to_idx:
            nan_mask = np.isnan(out)
            if idx_labeled:
                out = out.astype(np.object)
                out[~nan_mask] = self.wrapper.index[out[~nan_mask].astype(
                    np.int_)]
            else:
                out[nan_mask] = -1
                out = out.astype(np.int_)
        return self.wrapper.wrap_reduced(out, group_by=group_by, **kwargs)
Пример #2
0
    def reduce(self,
               reduce_func_nb: tp.ReduceFunc,
               *args,
               idx_arr: tp.Optional[tp.Array1d] = None,
               returns_array: bool = False,
               returns_idx: bool = False,
               to_index: bool = True,
               fill_value: tp.Scalar = np.nan,
               group_by: tp.GroupByLike = None,
               wrap_kwargs: tp.KwargsLike = None) -> tp.MaybeSeriesFrame:
        """Reduce mapped array by column/group.

        If `returns_array` is False and `returns_idx` is False, see `vectorbt.records.nb.reduce_mapped_nb`.
        If `returns_array` is False and `returns_idx` is True, see `vectorbt.records.nb.reduce_mapped_to_idx_nb`.
        If `returns_array` is True and `returns_idx` is False, see `vectorbt.records.nb.reduce_mapped_to_array_nb`.
        If `returns_array` is True and `returns_idx` is True, see `vectorbt.records.nb.reduce_mapped_to_idx_array_nb`.

        If `returns_idx` is True, must pass `idx_arr`. Set `to_index` to False to return raw positions instead
        of labels. Use `fill_value` to set the default value. Set `group_by` to False to disable grouping.
        """
        # Perform checks
        checks.assert_numba_func(reduce_func_nb)
        if idx_arr is None:
            if self.idx_arr is None:
                if returns_idx:
                    raise ValueError("Must pass idx_arr")
            idx_arr = self.idx_arr

        # Perform main computation
        col_map = self.col_mapper.get_col_map(group_by=group_by)
        if not returns_array:
            if not returns_idx:
                out = nb.reduce_mapped_nb(self.values, col_map, fill_value,
                                          reduce_func_nb, *args)
            else:
                out = nb.reduce_mapped_to_idx_nb(self.values, col_map, idx_arr,
                                                 fill_value, reduce_func_nb,
                                                 *args)
        else:
            if not returns_idx:
                out = nb.reduce_mapped_to_array_nb(self.values, col_map,
                                                   fill_value, reduce_func_nb,
                                                   *args)
            else:
                out = nb.reduce_mapped_to_idx_array_nb(self.values, col_map,
                                                       idx_arr, fill_value,
                                                       reduce_func_nb, *args)

        # Perform post-processing
        wrap_kwargs = merge_dicts(
            dict(name_or_index='reduce' if not returns_array else None,
                 to_index=returns_idx and to_index,
                 fillna=-1 if returns_idx else None,
                 dtype=np.int_ if returns_idx else None), wrap_kwargs)
        return self.wrapper.wrap_reduced(out, group_by=group_by, **wrap_kwargs)
Пример #3
0
    def reduce(self,
               reduce_func_nb,
               *args,
               default_val=np.nan,
               cast=None,
               **kwargs):
        """Reduce mapped array by column to a scalar value.

        See `vectorbt.records.nb.reduce_mapped_nb`.

        `**kwargs` will be passed to `vectorbt.base.array_wrapper.ArrayWrapper.wrap_reduced`."""
        checks.assert_numba_func(reduce_func_nb)

        result = nb.reduce_mapped_nb(self.mapped_arr, self.col_arr,
                                     len(self.wrapper.columns), default_val,
                                     reduce_func_nb, *args)
        if cast is not None:
            result = result.astype(cast)
        return self.wrapper.wrap_reduced(result, **kwargs)
Пример #4
0
    def reduce(self,
               reduce_func_nb: tp.ReduceFunc,
               *args,
               idx_arr: tp.Optional[tp.Array1d] = None,
               to_array: bool = False,
               to_idx: bool = False,
               idx_labeled: bool = True,
               default_val: float = np.nan,
               group_by: tp.GroupByLike = None,
               wrap_kwargs: tp.KwargsLike = None) -> tp.MaybeSeriesFrame:
        """Reduce mapped array by column.

        If `to_array` is False and `to_idx` is False, see `vectorbt.records.nb.reduce_mapped_nb`.
        If `to_array` is False and `to_idx` is True, see `vectorbt.records.nb.reduce_mapped_to_idx_nb`.
        If `to_array` is True and `to_idx` is False, see `vectorbt.records.nb.reduce_mapped_to_array_nb`.
        If `to_array` is True and `to_idx` is True, see `vectorbt.records.nb.reduce_mapped_to_idx_array_nb`.

        If `to_idx` is True, must pass `idx_arr`. Set `idx_labeled` to False to return raw positions instead
        of labels. Use `default_val` to set the default value. Set `group_by` to False to disable grouping.
        """
        # Perform checks
        checks.assert_numba_func(reduce_func_nb)
        if idx_arr is None:
            if self.idx_arr is None:
                if to_idx:
                    raise ValueError("Must pass idx_arr")
            idx_arr = self.idx_arr

        # Perform main computation
        col_map = self.col_mapper.get_col_map(group_by=group_by)
        if not to_array:
            if not to_idx:
                out = nb.reduce_mapped_nb(self.values, col_map, default_val,
                                          reduce_func_nb, *args)
            else:
                out = nb.reduce_mapped_to_idx_nb(self.values, col_map, idx_arr,
                                                 default_val, reduce_func_nb,
                                                 *args)
        else:
            if not to_idx:
                out = nb.reduce_mapped_to_array_nb(self.values, col_map,
                                                   default_val, reduce_func_nb,
                                                   *args)
            else:
                out = nb.reduce_mapped_to_idx_array_nb(self.values, col_map,
                                                       idx_arr, default_val,
                                                       reduce_func_nb, *args)

        # Perform post-processing
        if to_idx:
            nan_mask = np.isnan(out)
            if idx_labeled:
                out = out.astype(object)
                out[~nan_mask] = self.wrapper.index[out[~nan_mask].astype(
                    np.int_)]
            else:
                out[nan_mask] = -1
                out = out.astype(np.int_)
        wrap_kwargs = merge_dicts(
            dict(name_or_index='reduce' if not to_array else None),
            wrap_kwargs)
        return self.wrapper.wrap_reduced(out, group_by=group_by, **wrap_kwargs)
Пример #5
0
    def reduce(self, reduce_func_nb, *args, idx_arr=None, to_array=False, n_rows=None, to_idx=False,
               idx_labeled=True, default_val=np.nan, cast=None, group_by=None, **kwargs):
        """Reduce mapped array by column.

        If `to_array` is `False` and `to_idx` is `False`, see `vectorbt.records.nb.reduce_mapped_nb`.
        If `to_array` is `False` and `to_idx` is `True`, see `vectorbt.records.nb.reduce_mapped_to_idx_nb`.
        If `to_array` is `True` and `to_idx` is `False`, see `vectorbt.records.nb.reduce_mapped_to_array_nb`.
        If `to_array` is `True` and `to_idx` is `True`, see `vectorbt.records.nb.reduce_mapped_to_idx_array_nb`.

        If `to_array` is `True`, must pass `n_rows` indicating the number of elements in the array.
        If `to_idx` is `True`, must pass `idx_arr`. Set `idx_labeled` to `False` to return raw positions
        instead of labels. Use `default_val` to set the default value and `cast` to perform casting
        on the resulting pandas object. Set `group_by` to `False` to disable grouping.

        `**kwargs` will be passed to `vectorbt.base.array_wrapper.ArrayWrapper.wrap_reduced`."""
        # Perform checks
        checks.assert_numba_func(reduce_func_nb)
        if idx_arr is None:
            if self.idx_arr is None:
                if to_idx:
                    raise ValueError("Must pass idx_arr")
            idx_arr = self.idx_arr

        # Perform main computation
        group_arr, columns = self.wrapper.grouper.get_groups_and_columns(group_by=group_by)
        if group_arr is not None:
            col_arr = group_arr[self.col_arr]
        else:
            col_arr = self.col_arr
        if not to_array:
            if not to_idx:
                result = nb.reduce_mapped_nb(
                    self.mapped_arr,
                    col_arr,
                    len(columns),
                    default_val,
                    reduce_func_nb,
                    *args
                )
            else:
                result = nb.reduce_mapped_to_idx_nb(
                    self.mapped_arr,
                    col_arr,
                    idx_arr,
                    len(columns),
                    default_val,
                    reduce_func_nb,
                    *args
                )
        else:
            checks.assert_not_none(n_rows)
            if not to_idx:
                result = nb.reduce_mapped_to_array_nb(
                    self.mapped_arr,
                    col_arr,
                    len(columns),
                    n_rows,
                    default_val,
                    reduce_func_nb,
                    *args
                )
            else:
                result = nb.reduce_mapped_to_idx_array_nb(
                    self.mapped_arr,
                    col_arr,
                    idx_arr,
                    len(columns),
                    n_rows,
                    default_val,
                    reduce_func_nb,
                    *args
                )

        # Perform post-processing
        if to_idx:
            if idx_labeled:
                result_shape = result.shape
                result = result.flatten()
                mask = np.isnan(result)
                if mask.any():
                    # Contains NaNs
                    result[mask] = 0
                    result = result.astype(int)
                    result = self.wrapper.index[result].to_numpy()
                    result = result.astype(np.object)
                    result[mask] = np.nan
                else:
                    result = self.wrapper.index[result.astype(int)].to_numpy()
                result = np.reshape(result, result_shape)
            else:
                mask = np.isnan(result)
                result[mask] = -1
                result = result.astype(int)
        result = self.wrapper.wrap_reduced(result, group_by=group_by, **kwargs)
        if cast is not None:
            result = result.astype(cast)
        return result