def test_multi_columns_time_shift_feature(self): shift = Shift(shift=-2) df_multi = pd.DataFrame({"x0": [0, 1, 2, 3, 4, 5], "x1": [7, 8, 9, 10, 11, 12]}) expected_df = pd.DataFrame.from_dict( { f"x0__{shift_class_name}": [2, 3, 4, 5, np.nan, np.nan], f"x1__{shift_class_name}": [9, 10, 11, 12, np.nan, np.nan], } ) testing.assert_frame_equal(shift.fit_transform(df_multi), expected_df)
def horizon_shift(time_series: pd.DataFrame, horizon: Union[int, List[int]] = 5) -> pd.DataFrame: """Perform a shift of the original ``time_series`` for each time step between 1 and ``horizon``. Parameters ---------- time_series : pd.DataFrame, shape (n_samples, n_features), required The list of ``TimeSeriesFeature`` from which to compute the feature_extraction. horizon : int, optional, default: ``5`` It represents how much into the future is necessary to predict. This corresponds to the number of shifts that are going to be performed on y. Returns ------- y : pd.DataFrame, shape (n_samples, horizon) The shifted time series. Examples -------- >>> import pandas as pd >>> from gtime.model_selection import horizon_shift >>> X = pd.DataFrame(range(0, 5), index=pd.date_range("2020-01-01", "2020-01-05")) >>> horizon_shift(X, horizon=2) y_1 y_2 2020-01-01 1.0 2.0 2020-01-02 2.0 3.0 2020-01-03 3.0 4.0 2020-01-04 4.0 NaN 2020-01-05 NaN NaN >>> horizon_shift(X, horizon=[2]) y_2 2020-01-01 2.0 2020-01-02 3.0 2020-01-03 4.0 2020-01-04 NaN 2020-01-05 NaN """ horizon = range(1, horizon + 1) if isinstance(horizon, (int, float)) else horizon y = pd.DataFrame(index=time_series.index) for k in sorted(horizon): shift_feature = Shift(-k) y[f"y_{k}"] = shift_feature.fit_transform(time_series) return y
def test_random_ts_and_shifts(self, df: pd.DataFrame, shift: int): shift_feature = Shift(shift=shift) df_shifted = shift_feature.fit_transform(df) correct_df_shifted = self._correct_shift(df, shift)
def test_shift_transform(self, shift, expected): shift = Shift(shift=shift) testing.assert_frame_equal(shift.fit_transform(df), expected)