Calculate time difference between Pandas Dataframe indices

前端 未结 3 1691
执念已碎
执念已碎 2020-11-27 11:46

I am trying to add a column of deltaT to a dataframe where deltaT is the time difference between the successive rows (as indexed in the timeseries).

time             


        
3条回答
  •  醉梦人生
    2020-11-27 12:05

    Note this is using numpy >= 1.7, for numpy < 1.7, see the conversion here: http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-deltas

    Your original frame, with a datetime index

    In [196]: df
    Out[196]: 
                         value
    2012-03-16 23:50:00      1
    2012-03-16 23:56:00      2
    2012-03-17 00:08:00      3
    2012-03-17 00:10:00      4
    2012-03-17 00:12:00      5
    2012-03-17 00:20:00      6
    2012-03-20 00:43:00      7
    
    In [199]: df.index
    Out[199]: 
    
    [2012-03-16 23:50:00, ..., 2012-03-20 00:43:00]
    Length: 7, Freq: None, Timezone: None
    

    Here is the timedelta64 of what you want

    In [200]: df['tvalue'] = df.index
    
    In [201]: df['delta'] = (df['tvalue']-df['tvalue'].shift()).fillna(0)
    
    In [202]: df
    Out[202]: 
                         value              tvalue            delta
    2012-03-16 23:50:00      1 2012-03-16 23:50:00         00:00:00
    2012-03-16 23:56:00      2 2012-03-16 23:56:00         00:06:00
    2012-03-17 00:08:00      3 2012-03-17 00:08:00         00:12:00
    2012-03-17 00:10:00      4 2012-03-17 00:10:00         00:02:00
    2012-03-17 00:12:00      5 2012-03-17 00:12:00         00:02:00
    2012-03-17 00:20:00      6 2012-03-17 00:20:00         00:08:00
    2012-03-20 00:43:00      7 2012-03-20 00:43:00 3 days, 00:23:00
    

    Getting out the answer while disregarding the day difference (your last day is 3/20, prior is 3/17), actually is tricky

    In [204]: df['ans'] = df['delta'].apply(lambda x: x  / np.timedelta64(1,'m')).astype('int64') % (24*60)
    
    In [205]: df
    Out[205]: 
                         value              tvalue            delta  ans
    2012-03-16 23:50:00      1 2012-03-16 23:50:00         00:00:00    0
    2012-03-16 23:56:00      2 2012-03-16 23:56:00         00:06:00    6
    2012-03-17 00:08:00      3 2012-03-17 00:08:00         00:12:00   12
    2012-03-17 00:10:00      4 2012-03-17 00:10:00         00:02:00    2
    2012-03-17 00:12:00      5 2012-03-17 00:12:00         00:02:00    2
    2012-03-17 00:20:00      6 2012-03-17 00:20:00         00:08:00    8
    2012-03-20 00:43:00      7 2012-03-20 00:43:00 3 days, 00:23:00   23
    

提交回复
热议问题