Date time doubt

I have time data in dataframe in following format
08:29:55
17:43:09
21:53:00
16:51:23
17:40:49
.
.
.
millions of rows.
How can I give a label of time zone in each row? like

  • zone 1 = 00:00:01 to 03:00:00
  • zone 2 = 03:00:01 to 06:00:00
  • zone 3 = 06:00:01 to 09:00:00
  • zone 4 = 09:00:01 to 12:00:00
  • zone 5 = 12:00:01 to 15:00:00
  • zone 6 = 15:00:01 to 18:00:00
  • zone 7 = 18:00:01 to 21:00:00
  • zone 8 = 21:00:01 to 00:00:00
    Plz suggest solution.

The data you provided seems to be of a particular zone already(as any zones are not mention there), can you explain more what you are trying to do?

yeah, it is data of single country only. actually I used wrong keyword “zone”.
Actually I wanted to divide 24 hrs of day into 8 shifts as mentioned above . each shift of 3 hrs.
Then make column of shift next to time column & wanted to get shift assigned automatically according to that time.
Hope I was able to explain it better.
plz suggest How can I do it ?

So you can do this easily using the .apply() method of pandas dataframes. At first you have to define a function to get a timeshifts then pass the function in apply() method to get your column. See the demo code below.

import datetime
def shifts(time):
    if datetime.time(0, 0, 0) < time <= datetime.time(3,0,0):
        return 'Shift 1'
    elif ....:
        return .....
    .
    .
df['Shifts'] = df['time'].apply(shifts) #pass the function name
1 Like