I have a Pandas Dataframe (data) with a column ['Date'] with values like "yyyy-mm-dd HH:MM:SS" that I converted into Pandas Datetime format.
data['Date'] = pd.to_datetime(data['Date'])
How can I create a new categorical column ['Time'] with categories 'Early', 'Mid-day', 'Late' based on the hour of each day?
I tried this:
def time(x):
if '03:00:00' < x <= '11:00:00':
return 'Early'
elif '11:00:00' < x <= '17:00:00':
return 'Mid-day'
return 'Late'
data['Time'] = data['Date'].dt.time.apply(time)
But I get this error: " TypeError: '<' not supported between instances of 'str' and 'datetime.time' "
I would appreciate your help!