【问题标题】:Combine two time-series with different element of the datetime index in Python在 Python 中将两个时间序列与日期时间索引的不同元素结合起来
【发布时间】:2020-01-05 22:04:36
【问题描述】:

我下面有两个time-seriesdf1 有一个DateTime 格式的索引,其中包括仅日期,没有时间。 df2 有一个完整的日期时间索引,也是DateTime 格式。在完整数据中,df1 在行数上比df2 短很多。

如您所见,这两个数据集都跨越了 4 月 2 日至 6 日。但是,df1 会跳过一些日期,而在 df2 中,所有日期都可用。注意:在此示例中,仅跳过了奇数日期,但在完整数据中并非如此。

df1

    value1
date            
2016-04-02  16
2016-04-04  76
2016-04-06  23

df2

    value2
DateTime    
2016-04-02 07:45:00 257.96
2016-04-02 07:50:00 317.58
2016-04-02 07:55:00 333.39
2016-04-03 08:15:00 449.96
2016-04-03 08:20:00 466.42
2016-04-03 08:25:00 498.56
2016-04-04 08:10:00 454.73
2016-04-04 08:15:00 472.45
2016-04-04 08:20:00 489.85
2016-04-05 07:30:00 169.54
2016-04-05 07:35:00 276.13
2016-04-05 07:40:00 293.70
2016-04-06 07:10:00 108.05
2016-04-06 07:15:00 179.21
2016-04-06 07:20:00 201.80

我想按索引合并两个数据集。 df1 应该控制要保留的日期。 预期的结果如下。

    value2  value1
DateTime    
2016-04-02 07:45:00 257.96  16
2016-04-02 07:50:00 317.58  16
2016-04-02 07:55:00 333.39  16
2016-04-04 08:10:00 454.73  76
2016-04-04 08:15:00 472.45  76
2016-04-04 08:20:00 489.85  76
2016-04-06 07:10:00 108.05  23
2016-04-06 07:15:00 179.21  23
2016-04-06 07:20:00 201.80  23

这是我的尝试。

result= pd.concat([df1, df1], axis=1, sort=True).dropna(how='all')

但结果与我的预期不同。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这里可以使用DatetimeIndex.normalize 创建由没有时间的日期时间填充的新帮助列:

    df2['date'] = df2.index.normalize()
    

    或者如果日期使用DatetimeIndex.date:

    df2['date'] = df2.index.date
    

    然后使用merge 和默认的内连接:

    result= df1.merge(df2, left_index=True, right_on='date')
    print (result)
                         value1  value2       date
    DateTime                                      
    2016-04-02 07:45:00      16  257.96 2016-04-02
    2016-04-02 07:50:00      16  317.58 2016-04-02
    2016-04-02 07:55:00      16  333.39 2016-04-02
    2016-04-04 08:10:00      76  454.73 2016-04-04
    2016-04-04 08:15:00      76  472.45 2016-04-04
    2016-04-04 08:20:00      76  489.85 2016-04-04
    2016-04-06 07:10:00      23  108.05 2016-04-06
    2016-04-06 07:15:00      23  179.21 2016-04-06
    2016-04-06 07:20:00      23  201.80 2016-04-06
    

    或使用merge_asof,但它会由以前的匹配值合并,因此只有当始终将没有时间的日期时间与来自df2 的日期时间与来自dates 的df1 匹配时,才能像上面一样工作:

    result= pd.merge_asof(df2, df1, left_index=True, right_index=True)
    print (result)
                         value2  value1
    DateTime                           
    2016-04-02 07:45:00  257.96      16
    2016-04-02 07:50:00  317.58      16
    2016-04-02 07:55:00  333.39      16
    2016-04-03 08:15:00  449.96      16
    2016-04-03 08:20:00  466.42      16
    2016-04-03 08:25:00  498.56      16
    2016-04-04 08:10:00  454.73      76
    2016-04-04 08:15:00  472.45      76
    2016-04-04 08:20:00  489.85      76
    2016-04-05 07:30:00  169.54      76
    2016-04-05 07:35:00  276.13      76
    2016-04-05 07:40:00  293.70      76
    2016-04-06 07:10:00  108.05      23
    2016-04-06 07:15:00  179.21      23
    2016-04-06 07:20:00  201.80      23
    

    【讨论】:

      猜你喜欢
      • 2020-04-30
      • 2017-05-16
      • 1970-01-01
      • 2016-10-11
      • 2020-05-27
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      相关资源
      最近更新 更多