python - pandas get difference of 2 times assuming if end_time is lower than start_time it is the next day
问题描述
Assuming this is my dataframe:
date start_time end_time
1/1/2018 20:00 21:00
1/1/2018 23:00 1:00
I want to add another column, named duration
which is obviously end_time - start_time
My problem is that if I write something like:
pd.to_datetime(train_2.end_time,format='%H:%M:%S')-pd.to_timedelta(train_2.start_time))
It thinks that the second line is negative (as 23:00>1:00), while it's really positive as 1:00 refers to the next day (1/2/2018), so I want the duration to be 2 hours.
How can I achieve such a result?
Any help will be appreciated!
解决方案
您可以尝试通过转换为日期戳来进行减法,并且对于所有负值的例外情况,添加额外的天数
df['duration'] = pd.to_datetime(df.end_time) - pd.to_datetime(df.start_time)
df.loc[df.duration.dt.total_seconds() <0,'duration'] += pd.Timedelta(1,'D')
出去:
date start_time end_time duration
0 1/1/2018 20:00 21:00 01:00:00
1 1/1/2018 23:00 1:00 02:00:00
推荐阅读
- c# - 使用 DataTable 数据源确定 DataGridView 中行的类型
- ios - 在 iOS 中使用带有结帐功能的 Apple Pay 令牌付款
- sql - SQL 在以下情况下转换为全部大写
- json - Skillset 和 Indexer 不会将 Json 数组中的数据填充到 edm.ComplexType 字段中
- doctrine - 测试环境中的 Doctrine DBAL sql 记录器
- react-native - react-native babel错误未知选项:.name?
- android - 如何从 Kotlin 的另一个活动的片段中获得额外的意图?
- python - 在 pyqt5 的水平布局中正确拉伸小部件
- installation - 如何在cmake中将安装目标添加到Windows上的PATH变量
- google-cloud-firestore - 基于 Firestore 的聊天应用程序的推荐架构(可扩展)