您可以像这样使用 numpy 或 pandas(“pandas 版本”):
In [256]: s = pd.Series([2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35,
40, 45, 50, 55, 65, 75, 85, 86, 87, 88])
In [257]: df = pd.DataFrame({'time': s,
'time_diff': s.diff().shift(-1)}).set_index('time')
In [258]: df[df.time_diff - df.time_diff.shift(1) != 0].dropna()
Out[258]:
time_diff
time
2 1
10 5
55 10
85 1
如果您只想查看每个时间步的第一次出现,您还可以使用:
In [259]: df.drop_duplicates().dropna() # set take_last=True if you want the last
Out[259]:
time_diff
time
2 1
10 5
55 10
但是对于 pandas,您通常会使用 aDatetimeIndex
来使用内置的时间序列功能:
In [44]: a = [2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35,
40, 45, 50, 55, 65, 75, 85, 86, 87, 88]
In [45]: start_time = datetime.datetime.now()
In [46]: times = [start_time + datetime.timedelta(seconds=int(x)) for x in a]
In [47]: idx = pd.DatetimeIndex(times)
In [48]: df = pd.DataFrame({'data1': np.random.rand(idx.size),
'data2': np.random.rand(idx.size)},
index=idx)
In [49]: df.resample('5S') # resample to 5 Seconds
Out[49]:
data1 data2
2012-11-28 07:36:35 0.417282 0.477837
2012-11-28 07:36:40 0.536367 0.451494
2012-11-28 07:36:45 0.902018 0.457873
2012-11-28 07:36:50 0.452151 0.625526
2012-11-28 07:36:55 0.816028 0.170319
2012-11-28 07:37:00 0.169264 0.723092
2012-11-28 07:37:05 0.809279 0.794459
2012-11-28 07:37:10 0.652836 0.615056
2012-11-28 07:37:15 0.508318 0.147178
2012-11-28 07:37:20 0.261157 0.509014
2012-11-28 07:37:25 0.609685 0.324375
2012-11-28 07:37:30 NaN NaN
2012-11-28 07:37:35 0.736370 0.551477
2012-11-28 07:37:40 NaN NaN
2012-11-28 07:37:45 0.839960 0.118619
2012-11-28 07:37:50 NaN NaN
2012-11-28 07:37:55 0.697292 0.394946
2012-11-28 07:38:00 0.351824 0.420454
从我的角度来看,对于时间序列,Pandas 是迄今为止 Python 生态系统中可用的最好的库。不知道你真的想做什么,但我会试试 pandas。