Skip to content Skip to sidebar Skip to footer

How To Resample Timedeltas?

I have been running an experiment that outputs data with two columns: seconds since start of experiment (float) a measurement. (float) I would now like to load this into Pandas t

Solution 1:

You can use groupby with time // period to do this:

import pandas as pd
import numpy as np

t = np.random.rand(10000)*3600
t.sort()
v = np.random.rand(10000)

df = pd.DataFrame({"time":t, "value":v})

period = 5*60
s = df.groupby(df.time// period).value.mean()
s.index *= period

Solution 2:

I have the same structure of sensors data. First column is seconds since start of the experiment and rest of columns are value. here is the data structure:

time        x           y          z
      00.0159480.4039310.449005    -0.79686010.0360060.4039150.448029    -0.79539520.0558850.4049070.446548    -0.795853

here is what worked for me: convert the time to time delta:

df.time=pd.to_timedelta(df.time,unit="s")

set the time as index

df.set_index("time",inplace=True)

resample to the frequency you want

df.resample("40ms").mean()

Post a Comment for "How To Resample Timedeltas?"