1

I have time series data in pandas Dataframe with a 8hz sampling rate, i.e. 8 samples per second. I need to convert it to 16 hz data i.e. 16 samples per second. the index is in first column, in the format yyyy-mm-dd hh:mm:ss.ssssss. I am unable to resample using standard pandas command. can an one please help me with how to do this ? other answers speaks about hours 'H', minutes 'm', but can some one help me with where data is sub second.

View of the data

user5349
  • 123
  • 7

1 Answers1

1

You can actually do it the same way. Pandas resample supports values up to nanoseconds. Here are some values to keep in mind:

S        seconds
L        milliseconds
U        microseconds
N        nanoseconds

For your case, 8 samples per second is equivalent to 125 mellisecond, and 16 samples per second is 62.5 mellisecond.

Generate an example (8hz):

index = pd.date_range('1/1/2000', periods=9, freq='125L')
series = pd.Series(range(9), index=index)

Gives:

2000-01-01 00:00:00.000    0
2000-01-01 00:00:00.125    1
2000-01-01 00:00:00.250    2
2000-01-01 00:00:00.375    3
2000-01-01 00:00:00.500    4
2000-01-01 00:00:00.625    5
2000-01-01 00:00:00.750    6
2000-01-01 00:00:00.875    7
2000-01-01 00:00:01.000    8

Resample:

series = series.resample('62.5L').ffill()

Gives:

2000-01-01 00:00:00.000000    0
2000-01-01 00:00:00.062500    0
2000-01-01 00:00:00.125000    1
2000-01-01 00:00:00.187500    1
2000-01-01 00:00:00.250000    2
2000-01-01 00:00:00.312500    2
2000-01-01 00:00:00.375000    3
2000-01-01 00:00:00.437500    3
2000-01-01 00:00:00.500000    4
2000-01-01 00:00:00.562500    4
2000-01-01 00:00:00.625000    5
2000-01-01 00:00:00.687500    5
2000-01-01 00:00:00.750000    6
2000-01-01 00:00:00.812500    6
2000-01-01 00:00:00.875000    7
2000-01-01 00:00:00.937500    7
2000-01-01 00:00:01.000000    8
Mohammad
  • 3,276
  • 2
  • 19
  • 35
  • fill is working. but interpolate is not working, interpolate is filling with nan rather than the interpolated values. can you please help me out. – user5349 Sep 17 '21 at 18:09
  • @user5349 instead of `ffill` you just need to type `interpolate` this should work. If it doesn't, I would check that the type of the column is `float` (and not `object`). – Mohammad Sep 17 '21 at 20:39
  • what you said is correct, i converted to float, then it worked. thank you – user5349 Sep 19 '21 at 09:28