【问题标题】:Aligning a time series to start from 0 instead of timestamps for comparisons对齐时间序列以从 0 开始而不是时间戳以进行比较
【发布时间】:2020-05-04 08:09:10
【问题描述】:

我正在处理一个时间序列问题,我想匹配相似之处。由于我正在工作的时间序列有很多差距,我需要重新调整它以从 t0 - tn 开始,而不是实际的时间戳,以便它们对我的比较有用。

示例:
P1: (2019-12-22, 5, 0),(2019-12-24,3,1),(01-01-2020,2,0)
P2: (05-Jan-2020,5,0), (15-02-2020,4,1),(03-03-2020,3,0),(03-05-2020,5,1), (05-06-2020,2,0)

通过从 0 开始对齐:
P1: (t0, 5, 0),(t1,3,1),(t2,2,0)
P2: (t0,5,0), (t1,4,1),(t2,3,0),(t3,5,1),(t4,2,0)

对齐后从0开始到tn的系列看起来有点相似。此外,它们具有不等长和多变量序列。

目前,我正在为每组执行以下方法:

first_group_df["timestamp"] = pd.to_datetime(first_group_df["timestamp"]) # Create to datetime
first_group_df.sort_values(by="timestamp", inplace=True) # Sort it in the order of arrival
time_index = [i for i in range(0,len(first_group_df["timestamp"]))] # Index from 0 to number of datapoints
first_group_df["time_index"] = time_index #Add it as a column
first_group_df = first_group_df.set_index("time_index") #Make it index and then drop timestamp

是否有更好的方法将时间戳与整数索引对齐。我还认为排序后的简单 reset_index() 可能会起作用。我正在寻找更好的方法。

以下是其中一个 id 的参考示例数据框:

    pid  val  outcome            timestamp
0   112    5        1  22-12-2019 10:00:00
5   112    4        0  27-01-2020 11:00:00
10  112    2        1  29-01-2020 11:00:00
15  112    1        1  01-02-2020 10:00:00
20  112    5        1  01-03-2020 10:00:00

【问题讨论】:

    标签: python pandas time-series data-science data-analysis


    【解决方案1】:

    你可以使用Series.rank:

    df['t'] = df['timestamp'].rank()
    

    您没有指定的是如何对同一时间的行进行排名。 rank 函数接受 method 参数来处理关系。详情请查看文档。

    【讨论】:

      猜你喜欢
      • 2020-10-03
      • 2012-05-21
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      相关资源
      最近更新 更多