2

I have a pandas dataframe which consists of time series of multiple objects, e.g. each object has an ID and a list of values. Each row of the frame has the form Object_ID Time Value. Now I want to resample this dataframe via pd.resample(). What is the best way to do that treating every object separately?

An example - the original data frame is:

ID Time Value
0  0    1
0  1    1
0  2    1
3  1    4
3  2    6
3  3    6
5  10   0
5  11   1

An example output (resample from time steps 1s to 0.5s):

ID Time Value
0  0    1
0  0.5  1
0  1    1
0  1.5  1
0  2    1
0  2.5  1
3  1    4
3  1.5  5
3  2    6
3  2.5  6
3  3    6
3  3.5  6
5  10   0
5  10.5 0.5
5  11   1
5  11.5 1.5

(The interpolating is not crucial here, I think I can do that). Thanks!

Gemini
  • 475
  • 3
  • 12

1 Answers1

2

This almost works. The result is missing the very last row, and I don't see a concise way to fix it.

result = df.set_index(pd.to_timedelta(df['Time'], unit='s')).groupby('ID')\
.resample('500ms').asfreq().interpolate().reset_index(drop=True)

result['ID'] = result['ID'].astype(int)

# output
result

ID  Time  Value
 0   0.0      1.0
 0   0.5      1.0
 0   1.0      1.0
 0   1.5      1.0
 0   2.0      1.0
 3   1.0      4.0
 3   1.5      5.0
 3   2.0      6.0
 3   2.5      6.0
 3   3.0      6.0
 5  10.0      0.0
 5  10.5      0.5
 5  11.0      1.0
Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37