在Python中使用datetime索引的不同元素组合两个时间序列

在Python中使用datetime索引的不同元素组合两个时间序列,python,pandas,Python,Pandas,下面我有两个时间序列df1有一个DateTime格式的索引,该索引只包含日期,不包含时间df2有一个完整的日期时间索引,也是datetime格式。在完整数据中,df1在行数方面比df2短得多 如您所见,这两个数据集的时间跨度均为4月2日至6日。然而,df1跳过了一些日期,而在df2中,所有日期都可用。注意:在本例中,仅跳过奇数日期,但在完整数据中并非如此 df1 value1 date 2016-04-02 16 2016-04-04 76 2016-04-0

下面我有两个时间序列
df1
有一个
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')

但是结果与我预期的不同。

这里可以创建一个新的助手列,该列由datetimes填充,而不包含时间:

或者如果日期使用:

然后与默认内部联接一起使用:

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

或使用,但它会通过先前的匹配值进行合并,因此只有在始终将不带时间的日期时间从
df2
df1
中的
date
s进行匹配时,才能像上面一样工作:

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