如何在python中减去3小时的日期时间? [英] How can I subtract 3 hours of a datetime in python?

查看:117
本文介绍了如何在python中减去3小时的日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gmt中有两列日期时间,我需要减去此日期时间的三个小时。例如在第4行中,我需要在3小时内减去startdate,结果是:08/02/2018 17:20:0。在同一行4a中,我需要在3小时内减去结束日期,结果是:08/02/2018 21:50:0。

I have two columns with datetime in gmt and I need subtract threee hours of this datetime. For example in line 4 I need subtract startdate in 3 hours, the result it was: 08/02/2018 17:20:0. And in the same line 4 a i need to subtract enddate in 3 hours, the result it was: 08/02/2018 21:50:0.

cpf  day  startdate              enddate
1234  1   08/01/2018 12:50:0     08/01/2018 15:30:0
1234  1   08/01/2018 14:30:0     08/01/2018 15:40:0
1234  1   08/01/2018 14:50:0     08/01/2018 15:50:0
1234  2   08/02/2018 20:20:0     08/03/2018 00:50:0
1234  3   08/03/2018 01:00:0     08/03/2018 03:50:0
1235  1   08/01/2018 11:50:0     08/01/2018 15:20:0
5212  1   08/01/2018 14:50:0     08/01/2018 15:20:0



结果表



RESULT TABLE

cpf  day  startdate              enddate
1234  1   08/01/2018 09:50:0     08/01/2018 10:30:0
1234  1   08/01/2018 11:30:0     08/01/2018 10:40:0
1234  1   08/01/2018 11:50:0     08/01/2018 10:50:0
1234  2   08/02/2018 17:20:0     08/02/2018 21:50:0
1234  3   08/02/2018 22:00:0     08/03/2018 00:50:0
1235  1   08/01/2018 08:50:0     08/01/2018 10:20:0
5212  1   08/01/2018 11:50:0     08/01/2018 10:20:0

如何在python中做到这一点?

How can I do that in python?

PS:请注意结果。谢谢!

P.S.: Please, pay attention in results. Thank you!

推荐答案

您可以使用 timedelta

from datetime import timedelta

df['startdate'] = pd.to_datetime(df['startdate']) - timedelta(hours=3)
df['enddate'] = pd.to_datetime(df['enddate']) - timedelta(hours=3)

这篇关于如何在python中减去3小时的日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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