I have a question about float64 type -> datetime64 type conversion.

Asked 2 years ago, Updated 2 years ago, 115 views

Hello, everyone I want to convert from float64 type to datetime, but it doesn't work as I thought. First of all, in float64 format, the value is 44616.107037! If expressed in yyyy/mm/ddhh/mm/mm/ss form in Excel, it is well expressed as 2022/224 2:34:08 I tried to make it look like Python. It's not going as well as I thought.

        df1 = df['TIME'].apply(plus)
        df2 = df.insert(2, 'RealTIME', df1)
        df3 = pd.to_datetime(df['RealTIME'], format='%Y-%m-%d %H:%M:%S') 

        print(df3)

First of all, it was executed according to the above code, but the conversion to datetime64 works well The display value is 1970-01-0100:00:00.000044616, which is not what I expected Is there any other way to express it?

Thank you.

python datetime

2022-09-20 10:39

2 Answers

>>> import datetime

>>> d = 44616.107037


>>> s = datetime.date(1900, 1, 1)
>>> s + datetime.timedelta(days=d)
datetime.date(2022, 2, 26)


>>> s = datetime.datetime(1900, 1, 1)
>>> s + datetime.timedelta(days=d)
datetime.datetime(2022, 2, 26, 2, 34, 7, 996800)


2022-09-20 10:39

df3 = pd.to_datetime(df['RealTIME'], format='%Y/%m/%d %H:%M:%S') 


2022-09-20 10:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.