方案1
'4s'
与现有索引完全一致。当你
resample
,则可以从旧序列中获得表示,并可以进行插值。您要做的是创建一个索引,它是旧索引与新索引的并集。然后使用新索引进行插值和重新索引。
oidx = s.index
nidx = pd.date_range(oidx.min(), oidx.max(), freq='5s')
res = s.reindex(oidx.union(nidx)).interpolate('index').reindex(nidx)
res.plot(style='.-')
s.plot(style='o')
方案2A
如果你愿意放弃准确性,你可以
ffill
限制为
1
res = s.resample('5s').ffill(limit=1).interpolate()
res.plot(style='.-')
s.plot(style='o')
bfill
res = s.resample('5s').bfill(limit=1).interpolate()
res.plot(style='.-')
s.plot(style='o')
方案3
中等复杂度和准确性
nidx = pd.date_range(oidx.min(), oidx.max(), freq='5s')
res = s.reindex(nidx, method='nearest', limit=1).interpolate()
res.plot(style='.-')
s.plot(style='o')