python - 在Python中将两个时间序列与日期时间索引的不同元素组合

标签 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-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')

但是结果和我想象的不一样。

最佳答案

这里可以创建新的辅助列,由日期时间填充,没有时间 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 中没有时间的日期时间与 df1 中的 date 相匹配时,工作方式与上面相同:

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

关于python - 在Python中将两个时间序列与日期时间索引的不同元素组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600589/

相关文章:

python - 为什么 "X for X in DataFrame"只给出列名

python - 在 python 中运行多个 OLS 回归

python - 在嵌入式 Python 中使用 locale.setlocale 而不破坏 C 线程中的文件解析

python - 近似某个 epsilon 内的无限和

python - Tensorflow Tensorboard 默认端口

python - 如何使用python多次训练SVM分类器?

python - 如何在 Altair 的 HConcatChart 中配置图表位置

python - 如何 groupby() 在多列上聚合并重命名 Pandas 0.21+ 中的多索引?

python-3.x - 将数据附加到放置在字典中的 DataFrame 实例

python - Pandas - 将混合正数/负数的列转为正数