计算熊猫数据帧指数之间的时间差

我试图在数据框中添加一列 deltaT,其中 deltaT 是连续行之间的时间差(在时间序列中进行了索引)。

time                 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

期望的结果如下所示(以分钟为单位的 deltaT 单位) :

time                 value  deltaT


2012-03-16 23:50:00      1       0
2012-03-16 23:56:00      2       6
2012-03-17 00:08:00      3      12
2012-03-17 00:10:00      4       2
2012-03-17 00:12:00      5       2
2012-03-17 00:20:00      6       8
2012-03-20 00:43:00      7      23
88976 次浏览

注意,这里使用 numpy > = 1.7,对于 numpy < 1.7,请参见这里的转换: http://pandas.pydata.org/pandas-docs/dev/timeseries.html#time-deltas

带有日期时间索引的原始帧

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]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-03-16 23:50:00, ..., 2012-03-20 00:43:00]
Length: 7, Freq: None, Timezone: None

这是你想要的时间差64

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

在不考虑日差的情况下得出答案(你的最后一天是3/20,前一天是3/17) ,实际上是很棘手的

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

我们可以使用 to_series创建一个索引和值都等于索引键的序列,然后计算连续行之间的差异,这将导致 timedelta64[ns] dtype。在获得这个属性之后,通过 .dt属性,我们可以访问时间部分的秒属性,并最终将每个元素除以60以获得在几分钟内输出的结果(可以选择将第一个值填充为0)。

In [13]: df['deltaT'] = df.index.to_series().diff().dt.seconds.div(60, fill_value=0)
...: df                                 # use .astype(int) to obtain integer values
Out[13]:
value  deltaT
time
2012-03-16 23:50:00      1     0.0
2012-03-16 23:56:00      2     6.0
2012-03-17 00:08:00      3    12.0
2012-03-17 00:10:00      4     2.0
2012-03-17 00:12:00      5     2.0
2012-03-17 00:20:00      6     8.0
2012-03-20 00:43:00      7    23.0

简化:

当我们执行 diff:

In [8]: ser_diff = df.index.to_series().diff()


In [9]: ser_diff
Out[9]:
time
2012-03-16 23:50:00               NaT
2012-03-16 23:56:00   0 days 00:06:00
2012-03-17 00:08:00   0 days 00:12:00
2012-03-17 00:10:00   0 days 00:02:00
2012-03-17 00:12:00   0 days 00:02:00
2012-03-17 00:20:00   0 days 00:08:00
2012-03-20 00:43:00   3 days 00:23:00
Name: time, dtype: timedelta64[ns]

秒到分钟的转换:

In [10]: ser_diff.dt.seconds.div(60, fill_value=0)
Out[10]:
time
2012-03-16 23:50:00     0.0
2012-03-16 23:56:00     6.0
2012-03-17 00:08:00    12.0
2012-03-17 00:10:00     2.0
2012-03-17 00:12:00     2.0
2012-03-17 00:20:00     8.0
2012-03-20 00:43:00    23.0
Name: time, dtype: float64

如果假设您想要包含甚至 date部分,因为它以前被排除在外(只考虑了时间部分) ,那么 dt.total_seconds将给出经过的持续时间(以秒为单位) ,然后可以通过除法重新计算分钟。

In [12]: ser_diff.dt.total_seconds().div(60, fill_value=0)
Out[12]:
time
2012-03-16 23:50:00       0.0
2012-03-16 23:56:00       6.0
2012-03-17 00:08:00      12.0
2012-03-17 00:10:00       2.0
2012-03-17 00:12:00       2.0
2012-03-17 00:20:00       8.0
2012-03-20 00:43:00    4343.0    # <-- number of minutes in 3 days 23 minutes
Name: time, dtype: float64

>= Numpy version 1.7.0.

也可以 定型df.index.to_series().diff()timedelta64[ns](纳秒-默认 dtype)到 timedelta64[m](分钟)[ 变频器(类型转换等同于地板分割)]

df['ΔT'] = df.index.to_series().diff().astype('timedelta64[m]')


value      ΔT
time
2012-03-16 23:50:00      1     NaN
2012-03-16 23:56:00      2     6.0
2012-03-17 00:08:00      3    12.0
2012-03-17 00:10:00      4     2.0
2012-03-17 00:12:00      5     2.0
2012-03-17 00:20:00      6     8.0
2012-03-20 00:43:00      7  4343.0

(ΔT Dtype: float64)

如果要转换为 int,请在转换之前用 0填充 na

>>> df.index.to_series().diff().fillna(0).astype('timedelta64[m]').astype('int')


time
2012-03-16 23:50:00       0
2012-03-16 23:56:00       6
2012-03-17 00:08:00      12
2012-03-17 00:10:00       2
2012-03-17 00:12:00       2
2012-03-17 00:20:00       8
2012-03-20 00:43:00    4343
Name: time, dtype: int64


对于大熊猫版本 > 0.24.0. ,也可以转换为 熊猫可空整数数据类型(Int64)

>>> df.index.to_series().diff().astype('timedelta64[m]').astype('Int64')


time
2012-03-16 23:50:00    <NA>
2012-03-16 23:56:00       6
2012-03-17 00:08:00      12
2012-03-17 00:10:00       2
2012-03-17 00:12:00       2
2012-03-17 00:20:00       8
2012-03-20 00:43:00    4343
Name: time, dtype: Int64


Timedelta 数据类型支持大量的时间单位,以及可以被强制转换为其他任何单位的通用单位。

以下是日期单位:

Y   year
M   month
W   week
D   day

以下是时间单位:

h   hour
m   minute
s   second
ms  millisecond
us  microsecond
ns  nanosecond
ps  picosecond
fs  femtosecond
as  attosecond

如果你想要差异达到小数使用 true division,即,除以 Timedelta64(1,‘ m’)
例如,如 df 如下所示,

                     value
time
2012-03-16 23:50:21      1
2012-03-16 23:56:28      2
2012-03-17 00:08:08      3
2012-03-17 00:10:56      4
2012-03-17 00:12:12      5
2012-03-17 00:20:00      6
2012-03-20 00:43:43      7


检查下面 asyping (floor division)和 true division之间的区别。

>>> df.index.to_series().diff().astype('timedelta64[m]')
time
2012-03-16 23:50:21       NaN
2012-03-16 23:56:28       6.0
2012-03-17 00:08:08      11.0
2012-03-17 00:10:56       2.0
2012-03-17 00:12:12       1.0
2012-03-17 00:20:00       7.0
2012-03-20 00:43:43    4343.0
Name: time, dtype: float64


>>> df.index.to_series().diff()/np.timedelta64(1, 'm')
time
2012-03-16 23:50:21            NaN
2012-03-16 23:56:28       6.116667
2012-03-17 00:08:08      11.666667
2012-03-17 00:10:56       2.800000
2012-03-17 00:12:12       1.266667
2012-03-17 00:20:00       7.800000
2012-03-20 00:43:43    4343.716667
Name: time, dtype: float64