Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resample timedeltas?

Tags:

python

pandas

I have been running an experiment that outputs data with two columns:

  1. seconds since start of experiment (float)
  2. a measurement. (float)

I would now like to load this into Pandas to resample and plot the measurements. I've done this before, but those times my timestamps have been since epoch or in datetime (YYY-MM-DD HH:mm:ss) format. If I'm loading my first column as integers I'm unable to do

data.resample('5Min', how='mean')

. It also does not seem possible if I'd convert my first column to timedelta(seconds=...). My question is, is it possible to resample this data without subverting to epoch conversion?

like image 365
Ztyx Avatar asked Nov 28 '13 15:11

Ztyx


2 Answers

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
like image 87
HYRY Avatar answered Sep 24 '22 12:09

HYRY


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
      0     0.015948    0.403931    0.449005    -0.796860
      1     0.036006    0.403915    0.448029    -0.795395
      2     0.055885    0.404907    0.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()
like image 22
Rachel Shalom Avatar answered Sep 21 '22 12:09

Rachel Shalom