Пример #1
0
    def append_values(self,
                      values: np.ndarray,
                      index: pd.DatetimeIndex = None) -> 'TimeSeries':
        """
        Appends values to current TimeSeries, to the given indices.

        If no index is provided, assumes that it follows the original data.
        Does not update value if already existing indices are provided.

        Parameters
        ----------
        values
            An array with the values to append.
        index
            A `pandas.DateTimeIndex` for the new values (optional)

        Returns
        -------
        TimeSeries
            A new TimeSeries with the new values appended
        """
        if len(values) < 1:
            return self
        if isinstance(values, list):
            values = np.array(values)
        if index is None:
            index = pd.DatetimeIndex([self.end_time() + i * self.freq() for i in range(1, 1 + len(values))])
        raise_if_not(isinstance(index, pd.DatetimeIndex), 'Values must be indexed with a DatetimeIndex.', logger)
        raise_if_not(len(index) == len(values), 'Values and index must have same length.', logger)
        raise_if_not(self.time_index().intersection(index).empty, "Cannot add already present time index.", logger)
        new_indices = index.argsort()
        index = index[new_indices]
        # TODO do we really want that?
        raise_if_not(index[0] == self.end_time() + self.freq(),
                     'Appended index must start one time step after current one.', logger)
        if len(index) > 2:
            raise_if_not(index.inferred_freq == self.freq_str(),
                         'Appended index must have the same frequency as the current one.', logger)
        elif len(index) == 2:
            raise_if_not(index[-1] == index[0] + self.freq(),
                         'Appended index must have the same frequency as the current one.', logger)
        values = values[new_indices]
        new_series = pd.DataFrame(values, index=index)
        series = self._df.append(new_series)

        return TimeSeries(series, self.freq())
Пример #2
0
    def append_values(self,
                      values: np.ndarray,
                      index: pd.DatetimeIndex = None,
                      conf_lo: np.ndarray = None,
                      conf_hi: np.ndarray = None) -> 'TimeSeries':
        """
        Appends values to current TimeSeries, to the given indices.

        If no index is provided, assumes that it follows the original data.
        Does not add new confidence values if there were none first.
        Does not update value if already existing indices are provided.

        :param values: An array with the values to append.
        :param index: A DateTimeIndex for each value (optional).
        :param conf_lo: The lower confidence interval values (optional).
        :param conf_hi: The higher confidence interval values (optional).
        :return: A new TimeSeries with the new values appended
        """
        if len(values) < 1:
            return self
        if isinstance(values, list):
            values = np.array(values)
        if index is None:
            index = pd.DatetimeIndex([
                self.end_time() + i * self.freq()
                for i in range(1, 1 + len(values))
            ])
        raise_if_not(isinstance(index, pd.DatetimeIndex),
                     'Values must be indexed with a DatetimeIndex.', logger)
        raise_if_not(
            len(index) == len(values),
            'Values and index must have same length.', logger)
        raise_if_not(self.time_index().intersection(index).empty,
                     "Cannot add already present time index.", logger)
        new_indices = index.argsort()
        index = index[new_indices]
        # TODO do we really want that?
        raise_if_not(index[0] == self.end_time() + self.freq(), 'Appended index must start one time step ' \
                                                          'after current one.', logger)
        if len(index) > 2:
            raise_if_not(index.inferred_freq == self.freq_str(), 'Appended index must have ' \
                                                           'the same frequency as the current one.', logger)
        elif len(index) == 2:
            raise_if_not(index[-1] == index[0] + self.freq(), 'Appended index must have ' \
                                                        'the same frequency as the current one.', logger)
        values = values[new_indices]
        new_series = pd.Series(values, index=index)
        series = self._series.append(new_series)
        if conf_lo is not None and self._confidence_lo is not None:
            raise_if_not(
                len(index) == len(conf_lo),
                'Confidence intervals must have same length as index.', logger)
            conf_lo = conf_lo[new_indices]
            conf_lo = self._confidence_lo.append(
                pd.Series(conf_lo, index=index))
        if conf_hi is not None and self._confidence_hi is not None:
            raise_if_not(
                len(index) == len(conf_hi),
                'Confidence intervals must have same length as index.', logger)
            conf_hi = conf_hi[new_indices]
            conf_hi = self._confidence_hi.append(
                pd.Series(conf_hi, index=index))

        return TimeSeries(series, conf_lo, conf_hi)