pandas - future 警告 : Passing datetime64-dtype data

我有一个测量值及其相应时间戳的数据集,格式为 hh:mm:ss,其中 hh 可以 > 24 小时。

对于机器学习任务,需要对数据进行插值,因为有多个测量值分别具有不同的时间戳。 对于重采样和插值,我发现索引的数据类型应该是日期时间格式。 对于进一步的数据处理和机器学习任务,我将再次需要 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: Passing datetime64-dtype data to TimedeltaIndex is deprecated, will raise a TypeError in a future version Res_cont = pd.to_timedelta(Res_cont.index)

很明显,我提供的代码的最后一行有问题。我想知道如何更改此代码以防止 future 版本中出现类型错误。不幸的是,我不知道如何修复它。

也许你能帮忙?

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

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)

最佳答案

您可以使用 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.922807

关于pandas - future 警告 : Passing datetime64-dtype data to TimedeltaIndex is deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411059/

相关文章:

python - 用dask阅读时如何跳过坏行?

msbuild - 旧项目格式忽略 PackageReference 条件

python - 在 Tensorflow 2.0 中卡住和导出 TensorFlow 模型

python - 如何将文件传递给 unittest.mock.mock_open()?

ruby - Rails 5 使用 Devise 和 acts_as_tenant

apache-spark - 将数据从 Spark Structured Streaming 加载到

python - Pygame display init on headless Raspberry

python - 如何从数据集中创建锚定正负对来训练模型?

javascript - 使用 Vue I18n 和大内容文本 html 的最佳方式

python - 设置 TensorBoard 以在 Google Colab 中为 TensorF