简体   繁体   English

日期差异:Excel 与 Python 中的不同结果

[英]Date difference: different results in Excel vs. Python

I have a pandas dataframe with two dates columns with timestamp ( i want to keep time stamp) I want to get the difference in days between those two dates, I used the below.我有一个 pandas dataframe 有两个带有时间戳的日期列(我想保留时间戳)我想得到这两个日期之间的天数差异,我使用了下面的。 It works just fine.它工作得很好。

mergethetwo['diff_days']=(mergethetwo['todaydate']-mergethetwo['LastLogon']).dt.days

The doubt is, when I got the difference between those two dates in Excel, it gave me different number.怀疑是,当我在 Excel 中得到这两个日期之间的差异时,它给了我不同的数字。

In python for example the difference between例如在 python 中的区别

5/15/2020 1:48:00 PM (LastLogon) and 6/21/2020 12:00:00 AM(todaydate) is 36. 2020 年 5 月 15 日下午 1:48:00(上次登录)2020 年 6 月 21 日上午 12:00:00(今天)是 36。

However, in Excel using但是,在 Excel 中使用

DATEDIF =(LastLogon,todaydate,"d") 5/15/2020 1:48:00 PM and 6/21/2020 12:00:00 AM is 37 days ! DATEDIF =(LastLogon,todaydate,"d") 5/15/2020 1:48:00 PM6/21/2020 12:00:00 AM 是 37 天!

Why is the difference?为什么有区别? Which one should I trust?我应该相信哪一个? As I have 30,000 + rows I can't go through all od them to confirm.由于我有 30,000 + 行,我无法通过所有这些来确认 go。

Appreciate your support Thank you感谢您的支持谢谢

Excel DATEDIF with "D" seems to count "started" days ( dates , as the name of the function says...);带有"D"的 Excel DATEDIF似乎计算“开始”天数(日期,正如 function 的名称所说......); whilst the Python timedelta gives the actual delta in time - 36.425 days:而 Python timedelta 给出了实际的时间增量 - 36.425 天:

import pandas as pd

td = pd.to_datetime("6/21/2020 12:00:00 AM")-pd.to_datetime("5/15/2020 1:48:00 PM")
# Timedelta('36 days 10:12:00')

td.days
# 36

td.total_seconds() / 86400
# 36.425

You will get the same result if you do todaydate-LastLogon in Excel, without using any function.如果您在 Excel 中执行todaydate-LastLogon ,您将获得相同的结果,而不使用任何 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM