TimeSeriesDataFrame.slice_by_timestep

TimeSeriesDataFrame.slice_by_timestep(start_index: int | None = None, end_index: int | None = None) TimeSeriesDataFrame[源代码]

从每个时间序列中选择介于开始(包含)和结束(不包含)索引之间的子序列。

此操作等同于从每个时间序列中选择一个切片 [start_index : end_index],然后将这些切片合并为一个新的 TimeSeriesDataFrame。参见下面的示例。

返回原始数据的副本。这对于构建用于验证的保留集很有用。

参数:
  • start_index (intNone) – 每个时间序列切片的开始索引(包含)。负值从每个时间序列的末尾开始计数。设置为 None 时,切片从每个时间序列的开始处开始。

  • end_index (intNone) – 每个时间序列切片的结束索引(不包含)。负值从每个时间序列的末尾开始计数。设置为 None 时,切片包含每个时间序列的末尾。

返回:

ts_df – 一个新的时间序列 dataframe,包含原始时间序列中介于开始和结束索引之间的条目。

返回类型:

TimeSeriesDataFrame

示例

>>> 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

选择每个时间序列的第一个条目

>>> 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

选择每个时间序列的最后 2 个条目

>>> 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

选择除最后一个条目外的所有条目

>>> 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

复制整个 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