How to resample a grouped dataframe with zero order hold?

368 Views Asked by At

I have a dataframe with a bunch of different measurements (each have a unique ID in a measurements column), and measurement samples taken each 10ms.

Now I want to downsample all my data in order to "fake" a different sampling time, e.g. 40ms, for all measurements.

I implemented the resampling as shown below, but now I'm stuck: I do not want to perform an aggregation like .mean() on the resampler object, as this would process information from all samples in the respective 40ms bin.

df.set_index('timedelta', inplace=True)
df.index = pd.to_timedelta(df.index, unit='S')
df= df_resampled.groupby('measurement').resample('40ms')

Instead, I want to take only the first value within the resampled 40ms bin and hold it - basically, just pick the latest given value each 40ms.

Is there an elegant way of doing this?

1

There are 1 best solutions below

0
On

This answer was posted as an edit to the question How to resample a grouped dataframe with zero order hold? by the OP Rejaho under CC BY-SA 4.0.

Do:

df_resampled.groupby('measurement').resample('40ms').first()

See pandas' .first documentation.