Combine two time-series with different element of the datetime index in Python

后端 未结 1 336
一整个雨季
一整个雨季 2021-01-23 04:39

I have two time-series below. df1 has an index in a DateTime format which includes only date without time. df2

1条回答
  •  温柔的废话
    2021-01-23 05:30

    Here is possible create new helper column filled by datetimes without times with DatetimeIndex.normalize:

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

    Or if dates use DatetimeIndex.date:

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

    And then use merge with default inner join:

    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
    

    Or use merge_asof, but it merging by previous match values, so working same like above only if always match datetimes without times from df2 with dates from 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
    

    0 讨论(0)
提交回复
热议问题