def test_shift(timepoint, by): ret = _shift(timepoint, by=by) # check output type, pandas index types inherit from each other, # hence check for type equality here rather than using isinstance assert type(ret) is type(timepoint) # check if for a zero shift, input and output are the same if by == 0: assert timepoint == ret
def _predict_moving_cutoff( self, y, cv, X=None, update_params=False, return_pred_int=False, alpha=DEFAULT_ALPHA, ): """Make single-step or multi-step moving cutoff predictions Parameters ---------- y : pd.Series cv : temporal cross-validation generator X : pd.DataFrame update_params : bool return_pred_int : bool alpha : float or array-like Returns ------- y_pred = pd.Series """ if return_pred_int: raise NotImplementedError() fh = cv.get_fh() y_preds = [] cutoffs = [] # enter into a detached cutoff mode with self._detached_cutoff(): # set cutoff to time point before data self._set_cutoff(_shift(y.index[0], by=-1)) # iterate over data for new_window, _ in cv.split(y): y_new = y.iloc[new_window] # we cannot use `update_predict_single` here, as this would # re-set the forecasting horizon, instead we use # the internal `_update_predict_single` method y_pred = self._update_predict_single( y_new, fh, X=X, update_params=update_params, return_pred_int=return_pred_int, alpha=alpha, ) y_preds.append(y_pred) cutoffs.append(self.cutoff) return _format_moving_cutoff_predictions(y_preds, cutoffs)
def _get_last_window(self): """Select last window.""" # Get the start and end points of the last window. cutoff = self.cutoff start = _shift(cutoff, by=-self.window_length_ + 1) # Get the last window of the endogenous variable. y = self._y.loc[start:cutoff].to_numpy() # If X is given, also get the last window of the exogenous variables. X = self._X.loc[start:cutoff].to_numpy() if self._X is not None else None return y, X
def _get_last_window(self): """Select last window""" # get the start and end points of the last window cutoff = self.cutoff start = _shift(cutoff, by=-self.window_length_ + 1) # get the last window of the endogenous variable y = self._y.loc[start:cutoff].to_numpy() # if exogenous variables are given, also get the last window of # those if self._X is not None: X = self._X.loc[start:cutoff].to_numpy() else: X = None return y, X
def to_absolute(self, cutoff=None): """Return absolute values Parameters ---------- cutoff : pd.Period, pd.Timestamp, int, optional (default=None) Cutoff value is required to convert a relative forecasting horizon to an absolute one and vice versa. Returns ------- fh : ForecastingHorizon Absolute representation of forecasting horizon """ if not self.is_relative: return self._new() else: self._check_cutoff(cutoff) index = self.to_pandas() values = _shift(cutoff, by=index) return self._new(values, is_relative=False)