python - Python将小时数添加到数据框的日期时间索引
问题描述
我有一个日期时间索引数据框。我想增加几个小时。我的代码:
from datetime import datetime,timedelta
df.index =
DatetimeIndex(['2019-10-01 06:58:45', '2019-10-01 06:59:00',
'2019-10-01 06:59:15', '2019-10-01 06:59:30',
'2020-07-18 09:16:30', '2020-07-18 09:16:45'],
dtype='datetime64[ns]', name='',freq=None)
# add two hours to the index datetime
df.index = df.index.time+timedelta(hours=2)
目前的输出:
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
解决方案
不需要通话时间
df.index = df.index + pd.Timedelta('2 hour')
推荐阅读
- ruby-on-rails - `
': 未初始化的常量 Datagrid::Filters (NameError) - c# - 如何通过android文本到语音的代码设置语音?它为不可用的语音提供默认响应。我该如何解决这个问题?
- bash - 用于将特定文件从 csv 复制到具有相同文件夹层次结构的另一个目录的 Shell 脚本
- node.js - 查询文档不考虑时区mongodb
- python - 提取无类型输出
- mongodb - 在 MongoDB 中存储和查询 GeoJson 时出现的奇怪问题
- c# - .NET Core 2.2 Razor 页面代码背后创建文本文件,但无法将简单的行写入文件
- css - ASP.Net Core 中 fontawesome 样式表的 CDN 回退测试
- android - 更新android中的数据
- python - 解压数据框字典