0

please I need solution to this problem I have a field that is formatted in e.g 43390 which is general date format in excel. I need to format it to a date like "d/m/yyy"

here is the code I wrote :

trans_ data['DATE'] = pd.to_ datetime(trans_ data['DATE'], format='%d-%m-%Y')

but I have this error:

ValueError: time data '43390' does not match format '%d-%m-%Y' (match)

FObersteiner
  • 22,500
  • 8
  • 42
  • 72

2 Answers2

0

I tried to convert the "43390" in LibreOffice and it converted it to 2018-10-17 (the origin "0" is "30/12/1899"):

origin = pd.Timestamp("30/12/1899")
df["col"] = df["col"].apply(lambda x: origin + pd.Timedelta(days=x))
print(df)

Prints:

         col
0 2018-10-17
1 2019-11-02

df used:

     col
0  43390
1  43771

Screenshot:

enter image description here

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

The following code might help you.

from datetime import timedelta
import pandas as pd

excel_date = '43390'
excel_date = int(excel_date)
python_date = pd.to_datetime('1900-01-01') + timedelta(excel_date-2)

print(python_date)

The python_date object stores the date. Then you can change the format to the format you need.

GabrielP
  • 777
  • 4
  • 8