2017-04-05 115 views
0

我不知道爲什麼這段代碼會在所有日期生成01/01/1970,當我確定這不正確時。轉換爲熊貓時顯示的日期時間

我的原始文件:

Appreciation Start    Funding Date 
    41266       41432 
    41266       41715 
    41266       41533 
    41266       41505 
    41266       41444 
    41550       41513 
    41550       41451 

我的片段:

import pandas as pd 

df['Funding Date'] = pd.to_datetime(df['Funding Date']).apply(lambda x: x.strftime('%m/%d/%Y')if not pd.isnull(x) else '') 
df['Appreciation Start'] = pd.to_datetime(df['Appreciation Start']).apply(lambda x: x.strftime('%m/%d/%Y')if not pd.isnull(x) else '') 

df.to_excel('new.xlsx') 

在Excel:

Appreciation Start    Funding Date 
    01/01/1970     01/01/1970 
    01/01/1970     01/01/1970 
    01/01/1970     01/01/1970 
    ..........    .............. 

我該如何解決這個問題?

+0

在您的apply/lambda函數中,您試圖以mm/dd/yyyy格式解析日期,其中「41266」不是。 –

回答

1

熊貓不能將41266解碼爲日期。您可以在前一個零添加到日期,以便它看起來像041266.然後使用pd.to_datetime

df['Appreciation'] = '0'+df['Appreciation'].astype(str) 
df['Appreciation'] = pd.to_datetime(df['Appreciation'], format = '%m%d%y') 
0

1/1/1970 is the epoch date。當您的時間設置爲零秒並且您想將其格式化爲日期時,會發生什麼情況。

至於修復它,我會去@A-Za-z的回答。除非你要在幾秒鐘內測量時間,這將使這些日期在1/28/1970左右。