pythonpandas

Interpolating time series data for step values


I have time series data that looks like this (mm/dd hh:mm):

3.100   12/14 05:42
3.250   12/14 05:24
3.300   12/14 05:23
3.600   12/14 02:45
3.700   12/13 10:54
3.600   12/12 13:19
3.900   12/12 10:43

I need to interpolate it at 1 minute intervals. It will be a step chart, so the values should be the same until the new value.


Solution

  • If your goal is to make a step plot, no need to interpolate, just use matplotlib.pyplot.step:

    import matplotlib.pyplot as plt
    
    s = pd.Series(['12/14 05:42', '12/14 05:24', '12/14 05:23', '12/14 02:45', '12/13 10:54', '12/12 13:19', '12/12 10:43'],
                  index=[3.1, 3.25, 3.3, 3.6, 3.7, 3.6, 3.9])
    
    plt.step(pd.to_datetime(s, format='%m/%d %H:%M'), s.index)
    

    NB. assuming here the values are the index and the dates the series' values, which is a bit counterintuitive. Better use the date as index.

    Output:

    step plot from pandas series without interpolation

    Also, be aware that without a year, the default will be to use 1900 during the conversion to datetime, which might be unwanted. Better be explicit and add the exact year.


    If you really want to interpolate, use the date as index and asfreq:

    s = pd.Series([3.1, 3.25, 3.3, 3.6, 3.7, 3.6, 3.9],
                  index=['12/14 05:42', '12/14 05:24', '12/14 05:23', '12/14 02:45', '12/13 10:54', '12/12 13:19', '12/12 10:43'])
    
    s.index = pd.to_datetime(s.index, format='%m/%d %H:%M')
    
    out = s.asfreq('min', method='ffill')
    

    Output:

    1900-12-12 10:43:00    3.9
    1900-12-12 10:44:00    3.6
    1900-12-12 10:45:00    3.6
    1900-12-12 10:46:00    3.6
    1900-12-12 10:47:00    3.6
                          ... 
    1900-12-14 05:38:00    3.1
    1900-12-14 05:39:00    3.1
    1900-12-14 05:40:00    3.1
    1900-12-14 05:41:00    3.1
    1900-12-14 05:42:00    3.1
    Freq: T, Length: 2580, dtype: float64