转换时显示错误的日期

2024-06-10 18:12:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么这个片段是生产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
    ..........                ..............

我怎样才能解决这个问题?你知道吗


Tags: tolambdadfdatetimedateifnotstart
2条回答

1/1/1970 is the epoch date。这是当你的时间被设置为零秒,你想把它格式化为一个日期。你知道吗

至于修好它,我同意@A-Za-z的答案。除非你要用秒来测量时间,那么这些日期大概是1970年1月28日。你知道吗

熊猫无法将41266解读为日期。您可以在日期前面添加一个0,使其看起来像041266。然后使用pd.to\ U日期时间你知道吗

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

相关问题 更多 >