distribution of hours between a time range as datetime.time in python

Question:

I have two times and I want to make a list of all the hours between them using the same format in Python .

As I have

start_time: 08:00:00
end_time: 13:00:00

I want to make a list of hours between them. like this from start to end (08:00:00 – 13:00:00).

[
 {
  start: 08:00:00,
  end: 09:00:00
 },
 {
  start: 09:00:00,
  end: 10:00:00
 },
 {
  start: 10:00:00,
  end: 11:00:00
 },
 {
  start: 11:00:00,
  end: 12:00:00
 },
 {
  start: 12:00:00,
  end: 13:00:00
 }
]

one hour range distributed set between a time range. I’m using pandas but it’s quite handy with date distribution but couldn’t apply that on time. What can be the efficient way in python, I was wondering.

Asked By: Saikat Roy

||

Answers:

This should work, probably not the most efficient tho

start_time= datetime.strptime('08:00:00','%H:%M:%S')
end_time= datetime.strptime('13:00:00','%H:%M:%S')
[{'start':start_time.replace(hour=i),'end':start_time.replace(hour=i+1)} for i in range(start_time.hour,end_time.hour)]

Output:

[{'start': datetime.datetime(1900, 1, 1, 8, 0),
  'end': datetime.datetime(1900, 1, 1, 9, 0)},
 {'start': datetime.datetime(1900, 1, 1, 9, 0),
  'end': datetime.datetime(1900, 1, 1, 10, 0)},
 {'start': datetime.datetime(1900, 1, 1, 10, 0),
  'end': datetime.datetime(1900, 1, 1, 11, 0)},
 {'start': datetime.datetime(1900, 1, 1, 11, 0),
  'end': datetime.datetime(1900, 1, 1, 12, 0)},
 {'start': datetime.datetime(1900, 1, 1, 12, 0),
  'end': datetime.datetime(1900, 1, 1, 13, 0)}]
Answered By: Irsyaduddin

You can do:

Note: I m assuming your start_time, end_time are strings

from datetime import datetime
from datetime import timedelta 
f = '%H:%M:%S'
begin = datetime.strptime(start_time, f)
end = datetime.strptime(end_time, f)
duration = ((end-begin).seconds) // 3600
out = [{'start': (begin + timedelta(hours=i)).strftime(f),
       'end': (begin + timedelta(hours=i + 1)).strftime(f)} for i in
       range(duration)]

print(out):

[{'start': '08:00:00', 'end': '09:00:00'},
 {'start': '09:00:00', 'end': '10:00:00'},
 {'start': '10:00:00', 'end': '11:00:00'},
 {'start': '11:00:00', 'end': '12:00:00'},
 {'start': '12:00:00', 'end': '13:00:00'}]
Answered By: SomeDude

With :

start_time = '08:00:00'
end_time = '13:00:00'

s = pd.Series(pd.date_range(start_time, end_time, freq='H')
                .strftime('%H:%M:%S'))

out = (pd.DataFrame({'start': s, 'end':s.shift(-1)})
       .iloc[:-1]
       .to_dict('records')
       )

output:

[{'start': '08:00:00', 'end': '09:00:00'},
 {'start': '09:00:00', 'end': '10:00:00'},
 {'start': '10:00:00', 'end': '11:00:00'},
 {'start': '11:00:00', 'end': '12:00:00'},
 {'start': '12:00:00', 'end': '13:00:00'}]

You can easily generalize to any frequency. For example freq='2.5H'/freq='150min':

[{'start': '08:00:00', 'end': '10:30:00'},
 {'start': '10:30:00', 'end': '13:00:00'}]
Answered By: mozway

You can also use pd.date_range:

[{'start':(item - pd.Timedelta(hours=1)).strftime('%H:%M:%S') ,  'end':item.strftime('%H:%M:%S')} for item in list(pd.date_range('08:00:00', '13:00:00', freq='H'))[1:]]
Answered By: Nuri Taş