首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未来警告:不推荐将datetime64 64-dtype数据传递给TimedeltaIndex。

未来警告:不推荐将datetime64 64-dtype数据传递给TimedeltaIndex。
EN

Stack Overflow用户
提问于 2019-08-08 11:01:24
回答 1查看 2.8K关注 0票数 3

我有一个测量值的数据集和它们对应的hh:mm:ss格式的时间戳,其中hh可以是> 24小时。

对于机器学习任务,由于有不同时间戳的多个测量值,因此需要对数据进行插值。对于重采样和插值,我认为索引的dtype应该是datetime格式。对于进一步的数据处理和机器学习任务,我将再次需要timedelta格式。

以下是一些代码:

代码语言:javascript
复制
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错误。不幸的是,我根本不知道怎么修理它。

也许你能帮忙?

编辑:这里有一些任意的样本数据:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2019-08-08 11:03:00

可以使用DatetimeIndex.strftime将输出日期时间转换为HH:MM:SS格式:

代码语言:javascript
复制
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.922807
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57411059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档