[英]Calculating time difference between two different date formats in Python
I am trying to identify hours between two dates.我正在尝试确定两个日期之间的小时数。 Date format is not consistent between two columns两列之间的日期格式不一致
The below code works when the date format is similar.当日期格式相似时,以下代码有效。 How can I convert the UTC date format into normal date month year如何将 UTC 日期格式转换为正常日期月份年份
df['timebetween'] = (pd.to_datetime(df['datecolA'],dayfirst = True) - pd.to_datetime(df['datecolB'],dayfirst = True))
df['timebetween']= df['timebetween']/np.timedelta64(1,'h')
my data looks like below and I am interested in column timebetween which can be achieved from the above code if both date columns had same format我的数据如下所示,我对列 timebetween 感兴趣,如果两个日期列具有相同的格式,则可以从上面的代码中实现
datecolA datecolB timebetween
29/06/2020 08:30:00 2018-12-02T11:32:00.000Z x hours
29/06/2020 08:30:00 2018-12-04T14:00:00.000Z y hours
29/06/2020 08:30:00 2017-02-02T14:36:00.000Z z hours
29/06/2020 08:30:00 2017-02-02T14:36:00.000Z n hours
I think you need to remove UTC
from datecolB
:我认为您需要从datecolB
中删除UTC
:
df['datecolB'] = df.datecolB.dt.tz_localize(None)
# or extract the time delta directly
df['timebetween'] = (df.datecolA - df.datecolB.dt.tz_localize(None))/np.timedelta64(1,'h')
Output: Output:
datecolA datecolB timebetween
0 2020-06-29 08:30:00 2018-12-02 11:32:00+00:00 13796.966667
1 2020-06-29 08:30:00 2018-12-04 14:00:00+00:00 13746.500000
2 2020-06-29 08:30:00 2017-02-02 14:36:00+00:00 29825.900000
3 2020-06-29 08:30:00 2017-02-02 14:36:00+00:00 29825.900000
adding utc= true does the trick添加 utc=true 就可以了
df['timebetween'] = (pd.to_datetime(df['datecolA'],dayfirst = True,utc=True) - pd.to_datetime(df['datecolB'],dayfirst = True,utc=True))
df['timebetween']= df['timebetween']/np.timedelta64(1,'h')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.