Python: Adding hours to pandas timestamp

前端 未结 1 1017
情书的邮戳
情书的邮戳 2020-12-03 22:41

I read a csv file into pandas dataframe df and I get the following:

df.columns
Index([u\'TDate\', u\'Hour\', u\'SPP\'], dtype=\'object\')
>&g         


        
相关标签:
1条回答
  • 2020-12-03 22:52

    I think you can add to column TDate column Hour converted to_timedelta with unit='h':

    df = pd.DataFrame({'TDate':['2005-01-03','2005-01-04','2005-01-05'],
                       'Hour':[4,5,6]})
    
    df['TDate'] = pd.to_datetime(df.TDate)
    print (df)
       Hour      TDate
    0     4 2005-01-03
    1     5 2005-01-04
    2     6 2005-01-05
    
    df['TDate'] +=  pd.to_timedelta(df.Hour, unit='h')
    print (df)
       Hour               TDate
    0     4 2005-01-03 04:00:00
    1     5 2005-01-04 05:00:00
    2     6 2005-01-05 06:00:00
    
    0 讨论(0)
提交回复
热议问题