如何将对象转换为小时并添加到日期?

时间:2017-05-14 08:59:59

标签: pandas datetime timedelta

我有以下数据框:

correction = ['2.0','-2.5','4.5','-3.0']

date = ['2015-05-19 20:45:00','2017-04-29 17:15:00','2011-05-09 10:40:00','2016-12-18 16:10:00']

我想将修正转换为小时并将其添加到日期。我尝试了以下代码,但它得到了错误。

df['correction'] = pd.to_timedelta(df['correction'],unit='h')
df['date'] =pd.DatetimeIndex(df['date'])
df['date'] = df['date'] + df['correction']

我将修正转换为timedelta的错误为:

ValueError: no units specified

1 个答案:

答案 0 :(得分:2)

对我而言,作品被投放到floatcorrection

df['correction'] = pd.to_timedelta(df['correction'].astype(float),unit='h')
df['date'] = pd.DatetimeIndex(df['date'])
df['date'] = df['date'] + df['correction']
print (df)
         correction                date
0          02:00:00 2015-05-19 22:45:00
1 -1 days +21:30:00 2017-04-29 14:45:00
2          04:30:00 2011-05-09 15:10:00
3 -1 days +21:00:00 2016-12-18 13:10:00