Python regularise irregular time series with linear interpolation

后端 未结 4 1653
野的像风
野的像风 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:12

    You can do this with traces. First, create a TimeSeries with your irregular measurements like you would a dictionary:

    ts = traces.TimeSeries([
        (datetime(1992, 8, 27, 7, 46, 48), 28.0),
        (datetime(1992, 8, 27, 8, 0, 48), 28.2),
        ...
        (datetime(1992, 8, 27, 9, 3, 48), 30.0),
    ])
    

    Then regularize using the sample method:

    ts.sample(
        sampling_period=timedelta(minutes=15),
        start=datetime(1992, 8, 27, 8),
        end=datetime(1992, 8, 27, 9),
        interpolate='linear',
    )
    

    This results in the following regularized version, where the gray dots are the original data and the orange is the regularized version with linear interpolation.

    The interpolated values are:

    1992-08-27 08:00:00    28.189 
    1992-08-27 08:15:00    28.286  
    1992-08-27 08:30:00    28.377
    1992-08-27 08:45:00    28.848
    1992-08-27 09:00:00    29.891
    

提交回复
热议问题