pandasresumepandas-resample

How to resample pandas Dataframe time series data from 8hz to 16hz?


I have time series data in pandas Dataframe with a 8hz sampling rate, i.e. 8 samples per second. I need to convert it to 16 hz data i.e. 16 samples per second. the index is in first column, in the format yyyy-mm-dd hh:mm:ss.ssssss. I am unable to resample using standard pandas command. can an one please help me with how to do this ? other answers speaks about hours 'H', minutes 'm', but can some one help me with where data is sub second.

View of the data


Solution

  • You can actually do it the same way. Pandas resample supports values up to nanoseconds. Here are some values to keep in mind:

    S        seconds
    L        milliseconds
    U        microseconds
    N        nanoseconds
    

    For your case, 8 samples per second is equivalent to 125 mellisecond, and 16 samples per second is 62.5 mellisecond.

    Generate an example (8hz):

    index = pd.date_range('1/1/2000', periods=9, freq='125L')
    series = pd.Series(range(9), index=index)
    

    Gives:

    2000-01-01 00:00:00.000    0
    2000-01-01 00:00:00.125    1
    2000-01-01 00:00:00.250    2
    2000-01-01 00:00:00.375    3
    2000-01-01 00:00:00.500    4
    2000-01-01 00:00:00.625    5
    2000-01-01 00:00:00.750    6
    2000-01-01 00:00:00.875    7
    2000-01-01 00:00:01.000    8
    

    Resample:

    series = series.resample('62.5L').ffill()
    

    Gives:

    2000-01-01 00:00:00.000000    0
    2000-01-01 00:00:00.062500    0
    2000-01-01 00:00:00.125000    1
    2000-01-01 00:00:00.187500    1
    2000-01-01 00:00:00.250000    2
    2000-01-01 00:00:00.312500    2
    2000-01-01 00:00:00.375000    3
    2000-01-01 00:00:00.437500    3
    2000-01-01 00:00:00.500000    4
    2000-01-01 00:00:00.562500    4
    2000-01-01 00:00:00.625000    5
    2000-01-01 00:00:00.687500    5
    2000-01-01 00:00:00.750000    6
    2000-01-01 00:00:00.812500    6
    2000-01-01 00:00:00.875000    7
    2000-01-01 00:00:00.937500    7
    2000-01-01 00:00:01.000000    8