2

I have no idea if this is feasible in Pandas. I thought df.resample may do the work but no. Here's my objective:

I have a time-series in a DataFrame, df that looks like this:

            return
12:30:00 -0.000202
12:30:01 -0.000257
12:30:02 -0.000230
12:30:03 -0.000229
12:30:04 -0.000253
...
12:59:49  0.001491
12:59:50  0.001523
12:59:51  0.001503
12:59:52  0.001484
12:59:53  0.001513
12:59:54  0.001523
12:59:55  0.001527
12:59:56  0.001545
12:59:57  0.001532
12:59:58  0.001535
12:59:59  0.001566
13:00:00  0.001605

This is the plot:

enter image description here

Now you can you see that the time goes from 12:30:00 to 13:00:00. I want to re-scale or stretch the this time-series to have observations from 12:30:00 to 14:15:00. Hence I need to have 3.5 more entries in my original time-series... so 3.5 times more duplicate entries for each observation in my time-series. If it was only 3 time mores, then I would transform my data into array and use np.reshape() and then re-assign a time index but this won't work in this particular case. Any suggestions?

maxymoo
  • 35,286
  • 11
  • 92
  • 119
Plug4
  • 3,838
  • 9
  • 51
  • 79
  • You want to resample, with interpolation for non-integer time points. You'll need some signal-processing or statistical interpolation library. You'll need to decide/ experiment with which interpolating function you pick. – smci Oct 09 '15 at 00:18
  • Ya I see what you mean in the interpolation. The issue is that I don't know how to take a time-series that is spread over 30 minutes and spread over one-hour and 45 minute with `nan` in between numbers. Then the use of interpolation afterwards becomes easy. – Plug4 Oct 09 '15 at 04:12

1 Answers1

3

You can convert the datetime to unix epoch, multiply by your scale factor and then convert back (epoch calculation using How to get unix timestamp from numpy.datetime64)

df['epoch'] = df.index.astype(np.int64) // 10 ** 9     
start_epoch = df.epoch.iloc[0]
df['epochdelta']= df['epoch'] - start_epoch
df['newindex'] = pd.to_datetime((df.epochdelta * 3.5 + start_epoch),unit='s') 
Community
  • 1
  • 1
maxymoo
  • 35,286
  • 11
  • 92
  • 119