Option 1
That's because '4s'
aligns perfectly with your existing index. When you resample
, you get representation from your old series and are able to interpolate. What you want to do is to create an index that is the union of the old index with a new index. Then interpolate and reindex with a new index.
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')
Option 2A
If you are willing to forgo accuracy, you can ffill
with a limit of 1
res = s.resample('5s').ffill(limit=1).interpolate()
res.plot(style='.-')
s.plot(style='o')
Option 2B
Same thing with bfill
res = s.resample('5s').bfill(limit=1).interpolate()
res.plot(style='.-')
s.plot(style='o')
Option 3
Intermediate complexity and accuracy
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')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…