pandas -将一列中的秒数添加到另一列中的日期时间 [英] Pandas - Add seconds from a column to datetime in other column

查看:95
本文介绍了 pandas -将一列中的秒数添加到另一列中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的dataFrame,["StartDate" ,"duration"] StartDate列中的元素是datetime类型,而duration是整数.

I have a dataFrame with two columns, ["StartDate" ,"duration"] the elements in the StartDate column are datetime type, and the duration are ints.

类似:

StartDate  Duration
08:16:05    20  
07:16:01    20

我希望得到:

EndDate 
08:16:25
07:16:21

只需将秒数加到小时.

Simply add the seconds to the hour.

我正在检查有关它的一些想法,例如增量时间类型,并且所有这些日期时间都可以添加增量时间,但是到目前为止,我可以找到如何使用DataFrames进行此操作(以向量的方式,因为可能有可能遍历执行该操作的所有行).

I'd being checking some ideas about it like the delta time types and that all those datetimes have the possibilities to add delta times, but so far I can find how to do it with the DataFrames (in a vector fashion, cause It might be possible to iterate over all the rows performing the operation ).

推荐答案

考虑此df

    StartDate   duration
0   01/01/2017  135
1   01/02/2017  235

您可以像这样获得datetime列

You can get the datetime column like this

df['EndDate'] = pd.to_datetime(df['StartDate']) + pd.to_timedelta(df['duration'], unit='s')
df.drop('StartDate,'duration', axis = 1, inplace = True)

你得到

    EndDate             
0   2017-01-01 00:02:15 
1   2017-01-02 00:03:55 

带有您发布的示例数据框

with the sample dataframe that you posted

df['EndDate'] = pd.to_timedelta(df['StartDate']) + pd.to_timedelta(df['Duration'], unit='s')

这篇关于 pandas -将一列中的秒数添加到另一列中的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆