Search code examples
pythonpandasdownsampling

Downsampling data frome 25hz to 10hz


I have some time series data sampled at 25hz (25 samples per second)

time_in_secconds    data_vals
199.655    0.549038
199.696    0.83472
199.736    0.478569
199.776    0.114157
199.817    0.217603
199.858    0.701952
199.898    0.23409
199.938   -0.237923
199.979    0.337316
200.019    1.17735
200.059    1.42538

and I want to go down to 10 samples per second. I've been looking at doing this with pandas and have a few questions. The first one is what index object do I use for the times? is it https://pandas.pydata.org/docs/reference/api/pandas.TimedeltaIndex.seconds.html ?

The second is, after doing so, how do I actually go about resampling to get both time_in_seconds and data_vals at the new 10hz.


Solution

  • The documentation on the resample function contains much more useful information.

    Since time_in_secods measure times since some event, a TimedeltaIndex is the most appropriate here. Here's how to resample by averaging the original data points:

    # Original data @ 25Hz
    t = np.arange(199, 201, .04)
    df = pd.DataFrame({
        'time_in_seconds': t,
        'data_vals': np.random.uniform(-1, 1, len(t))
    })
    
    # Resample to 10Hz
    output = (
        df.set_index(pd.to_timedelta(df['time_in_seconds'], unit='s'))
          ['data_vals']
          .resample('100ms')
          .mean()
    )
    output.index = output.index.total_seconds()
    

    There are other resampling methods, such as min, max or weighted mean. Consult the documentation for more details.