Interpolating one time series onto another in pandas

前端 未结 3 751
别那么骄傲
别那么骄傲 2021-02-09 16:24

I have one set of values measured at regular times. Say:

import pandas as pd
import numpy as np
rng = pd.date_range(\'2013-01-01\', periods=12, freq=\'H\')
data          


        
3条回答
  •  眼角桃花
    2021-02-09 17:00

    Assume you would like to evaluate a time series ts on a different datetime_index. This index and the index of ts may overlap. I recommend to use the following groupby trick. This essentially gets rid of dubious double stamps. I then forward interpolate but feel free to apply more fancy methods

    def interpolate(ts, datetime_index):
        x = pd.concat([ts, pd.Series(index=datetime_index)])
        return x.groupby(x.index).first().sort_index().fillna(method="ffill")[datetime_index]
    

提交回复
热议问题