Exemplo n.º 1
0
    def all_minutes(self):
        """
        返回表示此日历中所有分钟的`DatetimeIndex`。
        """
        opens_in_ns = self._opens.values.astype(
            'datetime64[ns]',
        ).view('int64')

        closes_in_ns = self._closes.values.astype(
            'datetime64[ns]',
        ).view('int64')
        # compute_all_minutes假设每天仅包含连续分钟块
        dts = DatetimeIndex(
            compute_all_minutes(opens_in_ns, closes_in_ns),
            tz='utc',
        )
        # 如果有午休,则排除午休时段
        if self.use_lunch_break:
            # 需要使用utc时间
            utc_start = days_at_time(
                [dts[0].date()], self.lunch_break_start_time, self.tz).time[0]
            utc_end = days_at_time(
                [dts[0].date()], self.lunch_break_end_time, self.tz).time[0]
            locs = dts.indexer_between_time(
                utc_start, utc_end, include_start=True, include_end=True)
            return dts.delete(locs)
        else:
            return dts
Exemplo n.º 2
0
    def update(self,
               index: pd.DatetimeIndex,
               values: np.ndarray = None) -> 'TimeSeries':
        """
        Updates the TimeSeries instance with the new values provided.
        If indices are not in original TimeSeries, they will be discarded.
        Use `numpy.nan` to ignore a specific index in a series.

        Parameters
        ----------
        index
            A `pandas.DateTimeIndex` containing the indices to replace.
        values
            An array containing the values to replace (optional).

        Returns
        -------
        TimeSeries
            A new TimeSeries with updated values.
        """

        if isinstance(values, (list, range)):
            values = np.array(values)

        if values is not None and len(values.shape) == 1:
            values = values.reshape((len(values), 1))

        raise_if(values is None, "'values' parameter should not be None.", logger)
        raise_if(index is None, "Index must be filled.")
        if (values is not None):
            raise_if_not(len(values) == len(index), "The number of values must correspond "
                                                    "to the number of indices: {} != {}".format(len(values),
                                                                                                len(index)), logger)
            raise_if_not(self._df.shape[1] == values.shape[1], "The number of columns in values must correspond "
                                                               "to the number of columns in the current TimeSeries"
                                                               "instance: {} != {}".format(self._df.shape[1],
                                                                                           values.shape[1]))

        ignored_indices = [index.get_loc(ind) for ind in (set(index) - set(self.time_index()))]
        index = index.delete(ignored_indices)  # only contains indices that are present in the TimeSeries instance
        series = values if values is None else pd.DataFrame(np.delete(values, ignored_indices, axis=0), index=index)
        raise_if_not(len(index) > 0, "Must give at least one correct index.", logger)

        new_series = self.pd_dataframe()
        if series is not None:
            new_series.update(series)
            new_series = new_series.astype(self._df.dtypes)
        return TimeSeries(new_series, self.freq())
Exemplo n.º 3
0
    def update(self,
               index: pd.DatetimeIndex,
               values: np.ndarray = None,
               conf_lo: np.ndarray = None,
               conf_hi: np.ndarray = None,
               inplace: bool = True) -> 'TimeSeries':
        """
        Updates the Series with the new values provided.
        If indices are not in original TimeSeries, they will be discarded.
        At least one parameter other than index must be filled.
        Use np.nan to ignore a specific index in a series.

        It will raise an error if try to update a missing CI series

        :param index: A DateTimeIndex containing the indices to replace.
        :param values: An array containing the values to replace (optional).
        :param conf_lo: The lower confidence interval values to change (optional).
        :param conf_hi: The higher confidence interval values (optional).
        :param inplace: If True, do operation inplace and return None, defaults to True.
        :return: A TimeSeries with values updated

        TODO: Do we need this method? Where/how is it used? We should avoid mutating values at all cost.
        """
        raise_if_not(not (values is None and conf_lo is None and conf_hi is None), "At least one parameter must be filled " \
                                                                             "other than index", logger)
        raise_if_not(not index is None, "Index must be filled.")
        if (not values is None):
            raise_if_not(len(values) == len(index), \
                "The number of values must correspond to the number of indices: {} != {}".format(len(values), len(index)), logger)
        if (not conf_lo is None):
            raise_if_not(len(conf_lo) == len(index), \
                "The number of values must correspond to the number of indices: {} != {}".format(len(conf_lo), len(index)), logger)
        if (not conf_hi is None):
            raise_if_not(len(conf_hi) == len(index), \
                "The number of values must correspond to the number of indices: {} != {}".format(len(conf_hi), len(index)), logger)
        ignored_indices = [
            index.get_loc(ind) for ind in (set(index) - set(self.time_index()))
        ]
        index = index.delete(ignored_indices)
        series = values if values is None else pd.Series(
            np.delete(values, ignored_indices), index=index)
        conf_lo = conf_lo if conf_lo is None else pd.Series(
            np.delete(conf_lo, ignored_indices), index=index)
        conf_hi = conf_hi if conf_hi is None else pd.Series(
            np.delete(conf_hi, ignored_indices), index=index)
        raise_if_not(
            len(index) > 0, "Must give at least one correct index.", logger)
        if inplace:
            if series is not None:
                self._series.update(series)
            if conf_lo is not None:
                self._confidence_lo.update(conf_lo)
            if conf_hi is not None:
                self._confidence_hi.update(conf_hi)
            return None
        else:
            new_series = self.pd_series()
            new_lo = self.conf_lo_pd_series()
            new_hi = self.conf_hi_pd_series()
            if series is not None:
                new_series.update(series)
            if conf_lo is not None:
                new_lo.update(conf_lo)
            if conf_hi is not None:
                new_hi.update(conf_hi)
            return TimeSeries(new_series, new_lo, new_hi)