Jake Wagner
Jake Wagner

Reputation: 826

Wrong date displayed when converting in pandas

I do not know why this snippet is producing 01/01/1970 for all the dates when I am sure this is not accurate.

My Original File:

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

My snippet:

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')

In Excel:

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

How can I fix this?

Upvotes: 0

Views: 231

Answers (2)

Back2Basics
Back2Basics

Reputation: 7806

1/1/1970 is the epoch date. It's what happens when your time is set to zero seconds and you want to format it as a date.

As for fixing it I would go with @A-Za-z's answer. Unless you were going to measure time in seconds, which would make these dates around 1/28/1970.

Upvotes: 0

Vaishali
Vaishali

Reputation: 38415

Pandas is not able to decipher 41266 as a date. You can add a preceding zero to the date so that it looks like 041266. Then use pd.to_datetime

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

Upvotes: 1

Related Questions