Python 使用最近邻插值返回新值

Python 使用最近邻插值返回新值,python,pandas,nearest-neighbor,resampling,Python,Pandas,Nearest Neighbor,Resampling,我想通过使用最近邻插值来增加这些数据的采样 file.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:4

我想通过使用最近邻插值来增加这些数据的采样

file.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文件读入数据帧,并将其转换为日期时间索引数据帧。然后对其进行上采样并使用最近邻进行插值

df=pd.read\u csv(file.csv)
表单=“%Y-%m-%d%H:%m:%S”
df[''u time']=pd.to_datetime(df[''u time'].dt.strftime(form),exact=False)
df.设置索引(“U时间”,原地=真)
#现在向上采样
df=df.重采样('10s')。平均值()
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,但这里的情况并非如此

我是否遗漏了最近邻的工作原理?

df.resample(…).mean()创建了0.5的值。在进行重采样时,只需使用“最近”即可将缺失的值替换为序列中的最近邻值:

df = df.resample('10s').nearest()