gpt4 book ai didi

python - 日期差异出现错误 - Python

转载 作者:行者123 更新时间:2023-12-01 09:20:21 26 4
gpt4 key购买 nike

我有两种格式的日期值(6/13/2018 和 6-13-2018)。我必须计算日期差异。下面是我的工作。

问题:部分商品的天数不正确。

X['Date of Closing'] = X['Date of Closing'].str.replace('/','-')
X['Date of First Contact'] = X['Date of First Contact'].str.replace('/','-')

X['Date Difference'] = (pd.to_datetime(X['Date of Closing'])- pd.to_datetime(X['Date of First Contact'])).dt.days

示例:

Date of First Contact  | Date of Giving Proposal  | Date of Closing  \
0 13-01-2014 26-02-2014 26-02-2014
1 28-01-2014 2/2/2014 2-2-2014
2 11-1-2014 26-01-2014 26-01-2014
3 18-01-2014 18-01-2014 18-01-2014
4 14-01-2014 14-01-2014 14-01-2014
5 5-1-2014 14-01-2014 14-01-2014

输出:

44 - Correct

5 - Correct

-279 - Incorrect

0 - Correct

0 - Correct

-107 - Incorrect

最佳答案

我相信需要参数dayfirst=Trueformat:

X['Date Difference'] = (pd.to_datetime(X['Date of Closing'], dayfirst=True)- 
pd.to_datetime(X['Date of First Contact'], dayfirst=True)).dt.days
<小时/>
X['Date Difference'] = (pd.to_datetime(X['Date of Closing'], format='%d-%m-%Y')- 
pd.to_datetime(X['Date of First Contact'], format='%d-%m-%Y')).dt.days
<小时/>
print (X)
Date of First Contact Date of Giving Proposal Date of Closing \
0 13-01-2014 26-02-2014 26-02-2014
1 28-01-2014 2/2/2014 2-2-2014
2 11-1-2014 26-01-2014 26-01-2014
3 18-01-2014 18-01-2014 18-01-2014
4 14-01-2014 14-01-2014 14-01-2014
5 5-1-2014 14-01-2014 14-01-2014

Date Difference
0 44
1 5
2 15
3 0
4 0
5 9

关于python - 日期差异出现错误 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50838170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com