如何减去 Pandas Dataframe 中的两个日期时间值
我正在开发一个项目,我需要减去两个日期时间值来获取天数。数据示例如下所示:
ALARM_DATE CONT_DATE \
0 2020/06/18 00:00:00+00 2020/06/23 00:00:00+00
1 2020/06/01 00:00:00+00 2020/06/04 00:00:00+00
2 2020/08/10 00:00:00+00 2020/03/01 00:00:00+00
3 2020/03/31 00:00:00+00 2020/04/01 00:00:00+00
4 2020/04/14 00:00:00+00 2020/04/19 00:00:00+00
... ... ...
我尝试简单地减去这些值,但显然这不起作用。有人可以帮忙吗?
I’m working on a project and I need to subtract two date time values to get the number of days. A sample of the data can be seen below:
ALARM_DATE CONT_DATE \
0 2020/06/18 00:00:00+00 2020/06/23 00:00:00+00
1 2020/06/01 00:00:00+00 2020/06/04 00:00:00+00
2 2020/08/10 00:00:00+00 2020/03/01 00:00:00+00
3 2020/03/31 00:00:00+00 2020/04/01 00:00:00+00
4 2020/04/14 00:00:00+00 2020/04/19 00:00:00+00
... ... ...
I tried simply subtracting the values, but obviously that didn’t work. Can anyone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先将列转换为实际日期:
或者:
输出:
Convert your columns to actual dates first:
Or:
Output:
正如我在之前的回答中提出的此类问题,您还可以使用 dt() 访问器进行系列:
这具有从输出中删除“天”元素的优点。
As I put in my previous answer to this kind of question, you can also use the dt() accessor for series:
This has the advantage of removing the 'days' element from the output.