Python regularise irregular time series with linear interpolation

后端 未结 4 1654
野的像风
野的像风 2020-12-02 14:35

I have a time series in pandas that looks like this:

                     Values
1992-08-27 07:46:48    28.0  
1992-08-27 08:00:48    28.2  
1992-08-27 08:33         


        
4条回答
  •  春和景丽
    2020-12-02 15:15

    The same result that @mstringer gets can be achieved purely in pandas. The trick is to first resample by second, using interpolation to fill in the intermediate values (.resample('s').interpolate()), and then upsample in 15-minute periods (.resample('15T').asfreq()).

    import io
    import pandas as pd
    
    data = io.StringIO('''\
    Values
    1992-08-27 07:46:48,28.0  
    1992-08-27 08:00:48,28.2  
    1992-08-27 08:33:48,28.4  
    1992-08-27 08:43:48,28.8  
    1992-08-27 08:48:48,29.0  
    1992-08-27 08:51:48,29.2  
    1992-08-27 08:53:48,29.6  
    1992-08-27 08:56:48,29.8  
    1992-08-27 09:03:48,30.0
    ''')
    s = pd.read_csv(data, squeeze=True)
    s.index = pd.to_datetime(s.index)
    
    res = s.resample('s').interpolate().resample('15T').asfreq().dropna()
    print(res)
    

    Output:

    1992-08-27 08:00:00    28.188571
    1992-08-27 08:15:00    28.286061
    1992-08-27 08:30:00    28.376970
    1992-08-27 08:45:00    28.848000
    1992-08-27 09:00:00    29.891429
    Freq: 15T, Name: Values, dtype: float64
    

提交回复
热议问题