2017-04-05 134 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左右。