结果发现日期差异是错误的 - Python

Date difference coming out to be wrong - Python

我有两种格式的日期值(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