1

I have data from excel converted into pandas data frame: The column with dates is formatted to float and looks like this:

df = pd.DataFrame({'A': [44793.170428, 44793.182118, 44793.192697]})

enter image description here

How can I convert it to date time so that the result looks like in excel: enter image description here

toerag
  • 49
  • 3
  • 1
    Does this answer your question? [Convert Excel style date with pandas](https://stackoverflow.com/questions/38454403/convert-excel-style-date-with-pandas) – filippo Sep 26 '22 at 09:18

2 Answers2

2

Use pandas.TimedeltaIndex with datetime then pandas.to_datetime :

import datetime

df['A'] = (pd.TimedeltaIndex(df['A'], unit='d') + datetime.datetime(1899, 12, 30)).date

df['A'] = pd.to_datetime(df['A'])

# Output :

print(df)
            A
0  2022-08-20
1  2022-08-20
2  2022-08-20

print(df.dtypes)
A    datetime64[ns]
Timeless
  • 22,580
  • 4
  • 12
  • 30
0

Use can use:

df['A'] = [pd.to_datetime(x, unit='d') - pd.DateOffset(years=70) for x in df['A']]

Output:

                              A
0 2022-08-21 04:05:24.979200000
1 2022-08-21 04:22:14.995199488
2 2022-08-21 04:37:29.020800000
René
  • 4,594
  • 5
  • 23
  • 52