【问题标题】:Python add hours to datetime index of the dataframe [duplicate]Python将小时数添加到数据框的日期时间索引[重复]
【发布时间】:2021-01-19 06:12:08
【问题描述】:

我有一个日期时间索引数据框。我想增加几个小时。 我的代码:

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'

【问题讨论】:

    标签: python pandas dataframe datetime


    【解决方案1】:

    不需要通话时间

    df.index = df.index + pd.Timedelta('2 hour')
    

    【讨论】:

    猜你喜欢
    • 2019-05-14
    • 2021-03-12
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2015-11-23
    • 2023-04-08
    • 2019-03-16
    • 2018-07-31
    相关资源
    最近更新 更多