【问题标题】:How create a timestamp column from combine str timedelta/datetime columns ? Python Pandas如何从组合 str timedelta/datetime 列创建时间戳列?蟒蛇熊猫
【发布时间】:2020-11-01 15:57:04
【问题描述】:

我想从这些数据框创建一个时间戳列(Heure fin inter.):

Date        Heure       Durée (min) Heure debut inter.    Heure fin inter.
2019-08-11  13:50:00    00:10:00    2019-08-11 13:50:00   14:00:00
2019-08-11  15:00:00    00:30:00    2019-08-11 15:00:00   15:30:00
2019-08-11  16:30:00    00:05:00    2019-08-11 16:30:00   16:35:00
2019-08-11  18:00:00    00:15:00    2019-08-11 18:00:00   18:15:00 

为此,我尝试了以下代码:

df1['Durée (min)'] = pd.to_timedelta(df1['Durée (min)'], unit='min')
df1['Heure'] = df1['Heure'].dt.strftime('%H:%M:%S')
df1['Date'] = df1['Date'].dt.strftime('%Y-%m-%d')
df1['Heure fin inter.'] = df1['Heure'] + df1['Durée (min)']

df1['TS fin inter.'] = df1['Date'] + " " + df1['Heure fin inter.']

但是我在最后一行代码中遇到了这个错误:TypeError: unsupported operand type(s) for +: 'Timedelta' and 'str'

这里是df.info()

 #   Column                    Non-Null Count  Dtype          
---  ------                    --------------  -----          
 0   Date                      93 non-null     object         
 1   Heure                     93 non-null     object           
 5   Durée (min)               93 non-null     timedelta64[ns]
 9   Heure debut inter.        93 non-null     datetime64[ns] 
 10  Heure fin inter.          93 non-null     timedelta64[ns]

预期结果:

Date        Heure       Durée (min) Heure debut inter.    Heure fin inter.
2019-08-11  13:50:00    00:10:00    2019-08-11 13:50:00   2019-08-11 14:00:00
2019-08-11  15:00:00    00:30:00    2019-08-11 15:00:00   2019-08-11 15:30:00
2019-08-11  16:30:00    00:05:00    2019-08-11 16:30:00   2019-08-11 16:35:00
2019-08-11  18:00:00    00:15:00    2019-08-11 18:00:00   2019-08-11 18:15:00 

【问题讨论】:

    标签: python pandas dataframe datetime


    【解决方案1】:

    包括来自your other question 的信息,给df 点赞

    df
              date      hour  duration[min]
    0  2019-08-11  13:50:00             10
    1  2019-08-11  15:00:00             30
    2  2019-08-11  16:30:00              5
    3  2019-08-11  18:00:00             15
    

    您可以简单地计算开始和结束时间戳,例如

    df['interview_start'] = pd.to_datetime(df['date']+' '+df['hour'])
    df['interview_end'] = df['interview_start'] + pd.to_timedelta(df['duration[min]'], unit='min')
    
    # df
    #          date      hour  duration[min]     interview_start       interview_end
    # 0  2019-08-11  13:50:00             10 2019-08-11 13:50:00 2019-08-11 14:00:00
    # 1  2019-08-11  15:00:00             30 2019-08-11 15:00:00 2019-08-11 15:30:00
    # 2  2019-08-11  16:30:00              5 2019-08-11 16:30:00 2019-08-11 16:35:00
    # 3  2019-08-11  18:00:00             15 2019-08-11 18:00:00 2019-08-11 18:15:00
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 2016-10-16
      • 2018-12-13
      • 2021-11-09
      • 2022-07-19
      • 2014-03-03
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多