2

I am trying to pre-process timestamp in my data into four categories of the day.

This means i need to convert object data type to categories namely

Morning for 00:00:00 to 11:59:59

Afternoon for 12:00:00 to 15:59:59

Evening for 16:00:00 to 19:59:59

Night for 20:00:00 to 23:59:59

my timestamp data looks like

transaction timestamp
08:26:00
08:26:00
08:26:00
08:26:00
12:26:00
12:45:00
16:26:00
16:28:00
20:28:00
20:34:00

I expect the output to of the above mentioned column to be

time of day
Morning
Morning
Morning
Morning
Afternoon
Afternoon
Evening
Evening
Night
Night

How shall i clean this type of data and convert it to just 4 categories?

1 Answers1

4

You can convert values to timedeltas by to_timedelta and then use cut:

df['transaction timestamp'] = pd.to_timedelta(df['transaction timestamp'])
#if values are python object times convert to strings
#df['transaction timestamp'] = pd.to_timedelta(df['transaction timestamp'].astype(str))

b = pd.to_timedelta(['00:00:00','12:00:00','16:00:00','20:00:00', '24:00:00'])
l = ['Morning','Afternoon','Evening','Night']
df['time of day'] = pd.cut(df['transaction timestamp'], bins=b, labels=l)

print (df)
  transaction timestamp time of day
0              08:26:00     Morning
1              08:26:00     Morning
2              08:26:00     Morning
3              08:26:00     Morning
4              12:26:00   Afternoon
5              12:45:00   Afternoon
6              16:26:00     Evening
7              16:28:00     Evening
8              20:28:00       Night
9              20:34:00       Night
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252