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

Pandas 如何将对象转换为小时并添加到日期?,pandas,datetime,timedelta,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') d

我有以下数据框:

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

对于我来说,将作品转换为
float
correction

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

很高兴你能帮忙!周末愉快@Iris请不要忘记接受这个答案:-)