TimeSeriesDataFrame.slice_by_timestep

TimeSeriesDataFrame.slice_by_timestep(start_index: int | None = None, end_index: int | None = None) TimeSeriesDataFrame[source]

Select a subsequence from each time series between start (inclusive) and end (exclusive) indices.

This operation is equivalent to selecting a slice [start_index : end_index] from each time series, and then combining these slices into a new TimeSeriesDataFrame. See examples below.

Returns a copy of the original data. This is useful for constructing holdout sets for validation.

Parameters:
  • start_index (int or None) – Start index (inclusive) of the slice for each time series. Negative values are counted from the end of each time series. When set to None, the slice starts from the beginning of each time series.

  • end_index (int or None) – End index (exclusive) of the slice for each time series. Negative values are counted from the end of each time series. When set to None, the slice includes the end of each time series.

Returns:

ts_df – A new time series dataframe containing entries of the original time series between start and end indices.

Return type:

TimeSeriesDataFrame

Examples

>>> ts_df
                    target
item_id timestamp
0       2019-01-01       0
        2019-01-02       1
        2019-01-03       2
1       2019-01-02       3
        2019-01-03       4
        2019-01-04       5
2       2019-01-03       6
        2019-01-04       7
        2019-01-05       8

Select the first entry of each time series

>>> df.slice_by_timestep(0, 1)
                    target
item_id timestamp
0       2019-01-01       0
1       2019-01-02       3
2       2019-01-03       6

Select the last 2 entries of each time series

>>> df.slice_by_timestep(-2, None)
                    target
item_id timestamp
0       2019-01-02       1
        2019-01-03       2
1       2019-01-03       4
        2019-01-04       5
2       2019-01-04       7
        2019-01-05       8

Select all except the last entry of each time series

>>> df.slice_by_timestep(None, -1)
                    target
item_id timestamp
0       2019-01-01       0
        2019-01-02       1
1       2019-01-02       3
        2019-01-03       4
2       2019-01-03       6
        2019-01-04       7

Copy the entire dataframe

>>> df.slice_by_timestep(None, None)
                    target
item_id timestamp
0       2019-01-01       0
        2019-01-02       1
        2019-01-03       2
1       2019-01-02       3
        2019-01-03       4
        2019-01-04       5
2       2019-01-03       6
        2019-01-04       7
        2019-01-05       8