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

标签 python pandas time-series interpolation resampling

我不知道这在 Pandas 中是否可行。我认为 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 到 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/

相关文章:

python - 如何从本地计算机或网络资源将图像或图片嵌入到 jupyter notebook 中?

python - 使用 pandas 查找数据框中重复句子的数量

python - 如何高效地实现这种差分运算呢?

time-series - 动态时间扭曲和 Needleman-Wunsch 算法有什么区别?

python - 类型错误 : predict() missing 1 required positional argument: 'params'

python - 如何在 python 中绘制低对比度对象的轮廓?

python - 需要通过OpenCV Python检测书籍

python - 基于标签的重叠聚类(软聚类)

python - Pandas:根据另一列的匹配项替换列值

R如何从时间序列中提取月末行