【发布时间】:2020-06-22 23:04:40
【问题描述】:
我有一个如下的数据集“nodup”(未按时间排序)。 它是原始数据的子集,未排序。我需要每 15 分钟获取一次记录,例如 08:15、08:30、08:45... 但它仅在 occupancy = 1 时保留记录。
我需要做的是让 occupancy=0,并通过上一个和下一个值的平均值(来自同一设备)自动填充相关的新生成的 co2 和湿度。 :
device occupancy time co2 humidity
1 1 2019-06-27 10:17:22 818 40
2 1 2019-06-27 10:17:22 818 39
3 1 2019-06-27 08:00:05 625 40
4 1 2019-06-27 12:16:53 723 40
....
1 1 2019-06-28 10:17:22 818 40
2 1 2019-06-28 10:17:22 818 39
3 1 2019-06-28 08:02:05 625 40
4 1 2019-06-28 12:16:53 723 40
....
我想要的是作为例子(根据以前的数据生成15分钟的记录,按时间排序):
device occupancy time co2 humidity
1 1 2019-06-27 08:15 818 40
2 0 2019-06-27 08:15 XXX XX
3 0 2019-06-27 08:15 XXX XX
4 1 2019-06-27 08:15 723 40
....
1 1 2019-06-27 08:30 830 45
2 0 2019-06-27 08:30 XXX XX
我试过了
time_first =nodup['time'].min()
time_last = nodup['time'].max()
mux = pd.MultiIndex.from_product([pd.date_range(time_first, time_last,freq='15min'),
nodup['device'].unique()], names=['time', 'device'])
result = nodup.set_index(['time','device']).reindex(mux, fill_value=0).reset_index()
但它运行到:
ValueError: cannot handle a non-unique multi-index!
有人可以帮忙吗?当我查看原始数据时,该行中没有重复项
【问题讨论】:
-
为什么 10:17 变成 8:15 ?为什么 12:16 变成 8:15?为什么 8:02 变成了 8:15?
-
@JoranBeasley 谢谢回复。在这里粘贴数据时没有太注意。我修改了我的问题。
标签: python arrays pandas sorting