首页 > 解决方案 > Python:将不规则离散时间观察转换为规则时间序列

问题描述

我有一个如下的数据框,其中 elapsed_sum 只是 stop_at - started_at 的累积总和,以秒为单位。

{'started_at': {1418: Timestamp('2019-03-29 08:06:16.645000'),
  1417: Timestamp('2019-03-29 11:57:34.192000'),
  1423: Timestamp('2019-03-31 19:17:02.444000'),
  1422: Timestamp('2019-04-01 04:35:57.777000'),
  1421: Timestamp('2019-04-01 06:19:33.602000'),
  1420: Timestamp('2019-04-01 09:35:02.339000'),
  1425: Timestamp('2019-04-03 03:58:38.005000'),
  1424: Timestamp('2019-04-03 04:57:39.924000'),
  1419: Timestamp('2019-04-04 10:34:09.262000')},
 'stopped_at': {1418: Timestamp('2019-03-29 08:06:21.611000'),
  1417: Timestamp('2019-03-29 13:32:15.022000'),
  1423: Timestamp('2019-03-31 20:34:32.785000'),
  1422: Timestamp('2019-04-01 05:32:48.564000'),
  1421: Timestamp('2019-04-01 09:03:21.748000'),
  1420: Timestamp('2019-04-01 10:29:52.943000'),
  1425: Timestamp('2019-04-03 04:57:39.849000'),
  1424: Timestamp('2019-04-03 05:11:59.854000'),
  1419: Timestamp('2019-04-04 12:16:58.755000')},
 'elapsed_sum': {1418: 4.0,
  1417: 5684.0,
  1423: 10334.0,
  1422: 13744.0,
  1421: 23572.0,
  1420: 26862.0,
  1425: 30403.0,
  1424: 31262.0,
  1419: 37431.0}}

在此处输入图像描述 我想将其转换为常规时间序列,其中 x 轴定义如下:

start = df.started_at.min()
stop = df.stopped_at.max()
timetaken = stop-start

x = pd.timedelta_range(0, timetaken, freq = 'H')

标签: pythonpandastime-series

解决方案


推荐阅读