我有一个测量值的数据集和它们对应的hh:mm:ss格式的时间戳,其中hh可以是> 24小时。
对于机器学习任务,由于有不同时间戳的多个测量值,因此需要对数据进行插值。对于重采样和插值,我认为索引的dtype应该是datetime格式。对于进一步的数据处理和机器学习任务,我将再次需要timedelta格式。
以下是一些代码:
Res_cont = Res_cont.set_index('t_a') #t_a is the column of the timestamps for the measured variable a from a dataframe
#Then, I need to change datetime-format for resampling and interpolation, otherwise timedate are not like 00:15:00, but like 00:15:16 for example
Res_cont.index = pd.to_datetime(Res_cont.index)
#first, upsample to seconds, then interpolate linearly and downsample to 15min steps, lastly
Res_cont = Res_cont.resample('s').interpolate(method='linear').resample('15T').asfreq().dropna()
Res_cont.index = pd.to_timedelta(Res_cont.index) #Here is, where the error ocurred不幸的是,我得到了以下错误消息:
FutureWarning:不推荐将日期时间64-dtype数据传递给TimedeltaIndex,将在将来的版本Res_cont = pd.to_timedelta(Res_cont.index)中引发TypeError。
因此,显然,我提供的代码的最后一行存在问题。我想知道,如何更改此代码以防止将来版本中的Type错误。不幸的是,我根本不知道怎么修理它。
也许你能帮忙?
编辑:这里有一些任意的样本数据:
t_a = ['00:00:26', '00:16:16', '00:25:31', '00:36:14', '25:45:44']
a = [0, 1.3, 2.4, 3.8, 4.9]
Res_cont = pd.Series(data = a, index = t_a)发布于 2019-08-08 11:03:00
可以使用DatetimeIndex.strftime将输出日期时间转换为HH:MM:SS格式:
t_a = ['00:00:26', '00:16:16', '00:25:31', '00:36:14', '00:45:44']
a = [0, 1, 2, 3, 4]
Res_cont = pd.DataFrame({'t_a':t_a,'a':a})
print (Res_cont)
t_a a
0 00:00:26 0
1 00:16:16 1
2 00:25:31 2
3 00:36:14 3
4 00:45:44 4
Res_cont = Res_cont.set_index('t_a')
Res_cont.index = pd.to_datetime(Res_cont.index)
Res_cont=Res_cont.resample('s').interpolate(method='linear').resample('15T').asfreq().dropna()
Res_cont.index = pd.to_timedelta(Res_cont.index.strftime('%H:%M:%S'))
print (Res_cont)
a
00:15:00 0.920000
00:30:00 2.418351
00:45:00 3.922807https://stackoverflow.com/questions/57411059
复制相似问题