def _auto_fill(series: TimeSeries, **interpolate_kwargs) -> TimeSeries: """ This function fills the missing values in the TimeSeries `series`, using the `pandas.Dataframe.interpolate()` method. Parameters ---------- series The time series interpolate_kwargs Keyword arguments for `pandas.Dataframe.interpolate()`. See `the documentation <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.interpolate.html>`_ for the list of supported parameters. Returns ------- TimeSeries A new TimeSeries with all missing values filled according to the rules above. """ series_temp = series.pd_dataframe() # pandas interpolate wrapper, with chosen `method` if 'limit_direction' not in interpolate_kwargs: interpolate_kwargs['limit_direction'] = 'both' interpolate_kwargs['inplace'] = True series_temp.interpolate(**interpolate_kwargs) return TimeSeries.from_dataframe(series_temp, freq=series.freq)
def filter(self, series: TimeSeries): """ Computes a moving average of this series' values and returns a new TimeSeries. The returned series has the same length and time axis as `series`. (Note that this might create border effects). Behind the scenes the moving average is computed using :func:`pandas.DataFrame.rolling()` on the underlying DataFrame. Parameters ---------- series The a deterministic series to average Returns ------- TimeSeries A time series containing the average values """ filtered_df = (series.pd_dataframe(copy=False).rolling( window=self.window, min_periods=1, center=self.centered).mean()) return TimeSeries.from_dataframe( filtered_df, static_covariates=series.static_covariates, hierarchy=series.hierarchy, )
def transform(self, data: TimeSeries, *args, **kwargs) -> TimeSeries: super().transform(data, *args, **kwargs) def _boxcox_wrapper(col): idx = data._df.columns.get_loc(col.name) # get index from col name return boxcox(col, self._lmbda[idx]) return TimeSeries.from_dataframe(data._df.apply(_boxcox_wrapper))
def ts_inverse_transform( series: TimeSeries, lmbda: Union[Sequence[float], pd.core.series.Series]) -> TimeSeries: def _inv_boxcox_wrapper(col): idx = series._df.columns.get_loc( col.name) # get index from col name return inv_boxcox(col, lmbda[idx]) return TimeSeries.from_dataframe(series._df.apply(_inv_boxcox_wrapper))
def test_dummy_series(self): values = np.random.uniform(low=-10, high=10, size=100) ts = TimeSeries.from_dataframe(pd.DataFrame({"V1": values})) varima = VARIMA(trend="t") with self.assertRaises(ValueError): varima.fit(series=ts) if PMDARIMA_AVAILABLE: autoarima = AutoARIMA(trend="t") with self.assertRaises(ValueError): autoarima.fit(series=ts)
def fit(self, series: TimeSeries, future_covariates: Optional[TimeSeries] = None): # for VARIMA we need to process target `series` before calling DualForecastingModels' fit() method self._last_values = (series.last_values() ) # needed for back-transformation when d=1 for _ in range(self.d): series = TimeSeries.from_dataframe( df=series.pd_dataframe(copy=False).diff().dropna(), static_covariates=series.static_covariates, hierarchy=series.hierarchy, ) super().fit(series, future_covariates) return self
def create_timeseries( ts_data: pd.DataFrame, freq: str = None, value_cols: List[str] = None, ) -> TimeSeries: """Create a darts TimeSeries from a dataframe. The resulting TimeSeries has two columns. Column 0 is nick counts and column 1 is message count. """ if not value_cols: value_cols = ["nicks", "msgs"] return TimeSeries.from_dataframe( ts_data, time_col=None, value_cols=value_cols, freq=freq, )
return self._build_forecast_series(forecast) def predict_time_series(self, val: TimeSeries): super().predict(len(val)) x_test = val.time_index() # Reshape data x_test = np.array(x_test).reshape(x_test.shape[0], 1) forecast = self.model.predict(x_test) return self._build_forecast_series(forecast) @property def min_train_series_length(self) -> int: return 30 if __name__ == "__main__": import pandas as pd df = pd.read_csv('AirPassengers.csv', delimiter=",") series = TimeSeries.from_dataframe(df, 'Month', ['#Passengers']) train, val = series.split_before(pd.Timestamp('19540101')) model = LightGBM() model.fit(train) naive_forecast = model.predict_time_series(val) series.plot(label='actual') naive_forecast.plot(label='naive forecast (K=1)')