我不知道这在 Pandas 中是否可行。我认为 df.resample 可以完成这项工作,但没有。这是我的目标:
我在 DataFrame
、df
中有一个时间序列,如下所示:
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
这是情节:
现在您可以看到时间从 12:30:00 到 13:00:00
。我想重新缩放或拉伸(stretch)此时间序列以获取从 12:30:00 到 14:15:00
的观察结果。因此,我的原始时间序列中需要有 3.5 个以上的条目...因此,我的时间序列中的每个观察结果的重复条目要多 3.5 倍。如果只有 3 倍以上,那么我会将数据转换为数组并使用 np.reshape() ,然后重新分配时间索引,但这在这种特殊情况下不起作用。有什么建议吗?
最佳答案
您可以将日期时间转换为 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')
关于Python:通过非整数比例因子重新调整 pandas 中的时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33027713/