Python:按非整数比例因子重新缩放 pandas 中的时间序列
Python: Rescale time-series in pandas by non-integer scale-factor
我不知道这在 Pandas 中是否可行。我认为 df.resample
可以完成这项工作,但没有。这是我的 objective:
我在 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 to 13:00:00
开始。我想重新缩放或拉伸此时间序列以获取来自 12:30:00 to 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')
我不知道这在 Pandas 中是否可行。我认为 df.resample
可以完成这项工作,但没有。这是我的 objective:
我在 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 to 13:00:00
开始。我想重新缩放或拉伸此时间序列以获取来自 12:30:00 to 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')