0

I read a dataset that contains hours (i.e. 9:30 am) from a csv file. After exploring dataset, I saved it as a .xlsx format. Now, I want to add some fields to this dataset, however, hours data are in float numbers between 0 to 1.

In other example, I converted those numbers to string and it returned the hour. But with this format, this method just return float number in string format.

Consider this dataframe with an example of this data:

hours = [0.375, 0.41666666, 0.79166666, 0.79166666, 0.833333333, 0.375]
cities = ['AB', 'AC', 'AD', 'AF', 'AG', 'AT']
df = pd.DataFrame({'hour': hours, 'city': cities})

I have tried to convert to datetime the hours field

df['hours'] = [pd.to_datetime(i) for i in df['hour']]

However I got as a result:

1970-01-01 00:00:00

I was following this answer Converting a float to hh:mm format but time format is different from what I have in dataset.

Is there an specific method I can use in datetime or in pandas to convert those numbers to hours?

Ramiro Tormenta
  • 601
  • 1
  • 8
  • 18
  • Is each float a value in time of a 24-hour clock? Then `*24`. Is it a fraction of an hour? Then `*60` to get minutes. Basic clock math, really. – Jongware Feb 06 '18 at 22:01
  • Thanks for the tip @usr2564301. each float is a value in time of a 24-hour clock. Now I can figure out how to convert those to actual time. Thanks – Ramiro Tormenta Feb 06 '18 at 22:07

0 Answers0