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]})
How can I convert it to date time so that the result looks like in excel:
CodePudding user response:
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]
CodePudding user response:
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