def to_relative(self, cutoff=None): """Return relative 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 Relative representation of forecasting horizon """ if self.is_relative: return self._new() else: self._check_cutoff(cutoff) values = self.to_pandas() - cutoff if isinstance(self.to_pandas(), (pd.PeriodIndex, pd.DatetimeIndex)): values = _coerce_duration_to_int(values, unit=_get_unit(cutoff)) return self._new(values, is_relative=True)
def _align_seasonal(self, y): """Helper function to align seasonal components with y's time index""" shift = (-_get_duration( y.index[0], self._y_index[0], coerce_to_int=True, unit=_get_unit(self._y_index), ) % self.sp) return np.resize(np.roll(self.seasonal_, shift=shift), y.shape[0])
def test_coerce_duration_to_int(duration): ret = _coerce_duration_to_int(duration, unit=_get_unit(duration)) # check output type is always integer assert type(ret) in (pd.Int64Index, np.integer, int) # check result if isinstance(duration, pd.Index): np.testing.assert_array_equal(ret, range(3)) if isinstance(duration, pd.tseries.offsets.BaseOffset): ret == 3
def to_absolute_int(self, start, cutoff=None): """Return absolute values as zero-based integer index Parameters ---------- start : pd.Period, pd.Timestamp, int Start value 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 as zero-based integer index """ self._check_cutoff(start) absolute = self.to_absolute(cutoff).to_pandas() values = absolute - start if isinstance(absolute, (pd.PeriodIndex, pd.DatetimeIndex)): values = _coerce_duration_to_int(values, unit=_get_unit(cutoff)) return self._new(values, is_relative=False)