【发布时间】:2019-04-15 22:23:14
【问题描述】:
我想通过使用最近邻插值对这些数据进行上采样。
文件.csv
ProcessStepId,_time
0,2019-03-14 01:35:59.769
0,2019-03-14 01:37:59.076
0,2019-03-14 01:39:59.723
0,2019-03-14 01:42:00.145
1,2019-03-14 01:42:04.478
1,2019-03-14 01:43:59.818
1,2019-03-14 01:45:59.776
1,2019-03-14 01:47:59.802
到目前为止我的方法: 将 csv 文件读入数据帧并将其转换为 DateTime 索引数据帧。然后对其进行上采样并使用最近邻进行插值
df = pd.read_csv(file.csv)
form = '%Y-%m-%d %H:%M:%S'
df['_time'] = pd.to_datetime(df['_time'].dt.strftime(form), exact=False)
df.set_index('_time', inplace=True)
#Now upsample
df = df.resample('10s').mean()
df.interpolate(method='nearest', inplace=True)
我的输出如下所示:
_time, ProcessStepId
2019-03-14 01:35:50, 0.0
2019-03-14 01:36:00, 0.0
2019-03-14 01:36:10, 0.0
2019-03-14 01:36:20, 0.0
2019-03-14 01:36:30, 0.0
2019-03-14 01:36:40, 0.0
2019-03-14 01:36:50, 0.0
2019-03-14 01:37:00, 0.0
2019-03-14 01:37:10, 0.0
2019-03-14 01:37:20, 0.0
2019-03-14 01:37:30, 0.0
2019-03-14 01:37:40, 0.0
2019-03-14 01:37:50, 0.0
2019-03-14 01:38:00, 0.0
2019-03-14 01:38:10, 0.0
2019-03-14 01:38:20, 0.0
2019-03-14 01:38:30, 0.0
2019-03-14 01:38:40, 0.0
2019-03-14 01:38:50, 0.0
2019-03-14 01:39:00, 0.0
2019-03-14 01:39:10, 0.0
2019-03-14 01:39:20, 0.0
2019-03-14 01:39:30, 0.0
2019-03-14 01:39:40, 0.0
2019-03-14 01:39:50, 0.0
2019-03-14 01:40:00, 0.0
2019-03-14 01:40:10, 0.0
2019-03-14 01:40:20, 0.0
2019-03-14 01:40:30, 0.0
2019-03-14 01:40:40, 0.0
2019-03-14 01:40:50, 0.0
2019-03-14 01:41:00, 0.5
2019-03-14 01:41:10, 0.5
2019-03-14 01:41:20, 0.5
2019-03-14 01:41:30, 0.5
2019-03-14 01:41:40, 0.5
2019-03-14 01:41:50, 0.5
2019-03-14 01:42:00, 0.5
2019-03-14 01:42:10, 0.5
2019-03-14 01:42:20, 0.5
2019-03-14 01:42:30, 0.5
2019-03-14 01:42:40, 0.5
2019-03-14 01:42:50, 0.5
2019-03-14 01:43:00, 1.0
2019-03-14 01:43:10, 1.0
2019-03-14 01:43:20, 1.0
2019-03-14 01:43:30, 1.0
2019-03-14 01:43:40, 1.0
2019-03-14 01:43:50, 1.0
2019-03-14 01:44:00, 1.0
2019-03-14 01:44:10, 1.0
2019-03-14 01:44:20, 1.0
2019-03-14 01:44:30, 1.0
2019-03-14 01:44:40, 1.0
2019-03-14 01:44:50, 1.0
2019-03-14 01:45:00, 1.0
2019-03-14 01:45:10, 1.0
2019-03-14 01:45:20, 1.0
2019-03-14 01:45:30, 1.0
2019-03-14 01:45:40, 1.0
2019-03-14 01:45:50, 1.0
2019-03-14 01:46:00, 1.0
2019-03-14 01:46:10, 1.0
2019-03-14 01:46:20, 1.0
2019-03-14 01:46:30, 1.0
2019-03-14 01:46:40, 1.0
2019-03-14 01:46:50, 1.0
2019-03-14 01:47:00, 1.0
2019-03-14 01:47:10, 1.0
2019-03-14 01:47:20, 1.0
2019-03-14 01:47:30, 1.0
2019-03-14 01:47:40, 1.0
2019-03-14 01:47:50, 1.0
我希望所有的ProcessStepId 值都等于 1 或 0(理想情况下是整数),但这里有些行被分配了 0.5 的值(这对我的用例无效)。此外,我希望2019-03-14 01:42:04.478 之后的任何值都绝对等于 1,但这里的情况并非如此。
我是否遗漏了有关最近邻居如何工作的内容?
【问题讨论】:
标签: python pandas nearest-neighbor resampling