如何通过结合str timedelta / datetime列创建一个timestamp列?蟒蛇熊猫

时间:2020-11-01 15:57:04

标签: python pandas dataframe datetime

我想从这些数据框中创建一个时间戳列(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: +:'Timedelta'和'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 

1 个答案:

答案 0 :(得分:0)

在给定df的情况下,包括来自your other question的信息

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