Pandas - convert float to proper datetime or time object

Solution 1:

When you read the excel file specify the dtype of col itime as a str:

df = pd.read_excel("test.xlsx", dtype={'itime':str})

then you will have a time column of strings looking like:

df = pd.DataFrame({'itime':['2300', '0100', '0500', '1000']})

Then specify the format and convert to time:

df['Time'] = pd.to_datetime(df['itime'], format='%H%M').dt.time

    itime   Time
0   2300    23:00:00
1   0100    01:00:00
2   0500    05:00:00
3   1000    10:00:00

Solution 2:

Just addon to Chris answer, if you are unable to convert because there is no zero in the front, apply the following to the dataframe.

df['itime'] = df['itime'].apply(lambda x: x.zfill(4))

So basically is that because the original format does not have even leading digit (4 digit). Example: 945 instead of 0945.