Dave Will
Dave Will

Reputation: 95

Converting float into datatime64[ns]

This question is related to the following one here Hours and time converting

I have got the following array with the corresponding values form the previous question:

a = [[7.15, 7.45, 9.30, 10.45, 13.45, 15.15, 15.45, 21.30]]

its values are floats and they represent hours of a day, e.g. 7.15 is equal to 7:15. Now I am using the following formula to in pandas to make a comaprison:

df.loc[([df['orders_time'] >= a[0]) & (df['orders_time'] <= a[1]), 'new_time'] = 10

It returns an error saying:

Invalid comparison between dtype=datetime64[ns] and float64

I tried to change the format of the values in a I was not able to run it.

Upvotes: 0

Views: 200

Answers (1)

dubbbdan
dubbbdan

Reputation: 2740

You can convert a into a list of times.

a = [pd.to_datetime(i, format = '%H.%M').time() for i in a[0]]

then you can compare time to time using:

df.loc[([df['orders_time'].dt.time >= a[0]) & (df['orders_time'].dt.time <= a[1]), 'new_time'] = 10 

Upvotes: 1

Related Questions