Python:通过非整数缩放因子重新调整pandas中的时间序列

时间:2015-10-08 23:44:17

标签: python pandas time-series interpolation resampling

我不知道这在熊猫中是否可行。我以为df.resample可以做这项工作,但不是。这是我的目标:

我在DataFramedf中有一个如下所示的时间序列:

            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

这是情节:

enter image description here

现在你可以看到时间从12:30:00 to 13:00:00开始。我想重新缩放或延伸这个时间序列以获得12:30:00 to 14:15:00的观察结果。因此,我需要在原始时间序列中增加3.5个条目...因此,在我的时间序列中,每次观察的重复条目多3.5倍。如果它只有3倍多,那么我会将我的数据转换为数组并使用np.reshape()然后重新分配时间索引,但这在这种特殊情况下不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

您可以将日期时间转换为unix时期,乘以比例因子然后转换回来(使用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')