The following code gives me 90 minutes time blocks for a given start time and end time. If we see the first row the 90 minute time block starts from 11/07/2021 04:52
and ends at 11/07/2021 06:22
. However, it can also start from 11/07/2021 04:53
and end at 11/07/2021 06:23
. Also, start from 11/07/2021 04:54
and end at 11/07/2021 06:24
. The maximum value of start time can be difference of end time and the interval (i.e., end - interval
). This also means the end time cannot exceed the value stored in object end
in the code below, and code below ensures that.
So, how can we increase the start time by 1 minute until it reaches its maximum value?
from datetime import datetime, timedelta
def datetime_range(start, end, delta):
current = start
while current < end:
yield current
current = delta
start = datetime(2021,7,11,4,52,0)
end = start pd.Timedelta(minutes=1198)
interval = timedelta(minutes=90)
ts = [dt.strftime('%d/%m/%Y %H:%M') for dt in datetime_range(start, end, interval)]
print(dts)
task = pd.DataFrame(columns=['ST','ET'])
def splitIntoBlocks(dts):
for i in range(len(dts)-1):
task.loc[i, ['ST']]=dts[i]
task.loc[i, ['ET']]=dts[i 1]
task['alpha'] = (pd.to_datetime(task['ST'], format="%d/%m/%Y %H:%M")-pd.to_datetime(start, format="%d/%m/%Y %H:%M"))/timedelta(minutes=1)
task['MID'] = 1
splitIntoBlocks(dts)
print(task.head(40))
Output
['11/07/2021 04:52', '11/07/2021 06:22', '11/07/2021 07:52', '11/07/2021 09:22', '11/07/2021 10:52', '11/07/2021 12:22', '11/07/2021 13:52', '11/07/2021 15:22', '11/07/2021 16:52', '11/07/2021 18:22', '11/07/2021 19:52', '11/07/2021 21:22', '11/07/2021 22:52', '12/07/2021 00:22']
ST ET alpha MID
0 11/07/2021 04:52 11/07/2021 06:22 0.0 1
1 11/07/2021 06:22 11/07/2021 07:52 90.0 1
2 11/07/2021 07:52 11/07/2021 09:22 180.0 1
3 11/07/2021 09:22 11/07/2021 10:52 270.0 1
4 11/07/2021 10:52 11/07/2021 12:22 360.0 1
5 11/07/2021 12:22 11/07/2021 13:52 450.0 1
6 11/07/2021 13:52 11/07/2021 15:22 540.0 1
7 11/07/2021 15:22 11/07/2021 16:52 630.0 1
8 11/07/2021 16:52 11/07/2021 18:22 720.0 1
9 11/07/2021 18:22 11/07/2021 19:52 810.0 1
10 11/07/2021 19:52 11/07/2021 21:22 900.0 1
11 11/07/2021 21:22 11/07/2021 22:52 990.0 1
12 11/07/2021 22:52 12/07/2021 00:22 1080.0 1
CodePudding user response:
Doing what you ask does not require your dts
array at all.
import pandas as pd
from datetime import datetime, timedelta
start = datetime(2021,7,11,4,52,0)
end = start pd.Timedelta(minutes=1198)
interval = timedelta(minutes=90)
task = pd.DataFrame(columns=['ST','ET'])
def splitIntoBlocks(now):
for i in range(1198):
task.loc[i, ['ST']]=now
task.loc[i, ['ET']]=now interval
task['alpha'] = (task['ST'] - start)/timedelta(minutes=1)
task['MID'] = 1
now = timedelta(minutes=1)
splitIntoBlocks(start)
print(task.head(40))
Output:
ST ET alpha MID
0 2021-07-11 04:52:00 2021-07-11 06:22:00 0.0 1
1 2021-07-11 04:53:00 2021-07-11 06:23:00 1.0 1
2 2021-07-11 04:54:00 2021-07-11 06:24:00 2.0 1
3 2021-07-11 04:55:00 2021-07-11 06:25:00 3.0 1
4 2021-07-11 04:56:00 2021-07-11 06:26:00 4.0 1
5 2021-07-11 04:57:00 2021-07-11 06:27:00 5.0 1
6 2021-07-11 04:58:00 2021-07-11 06:28:00 6.0 1
7 2021-07-11 04:59:00 2021-07-11 06:29:00 7.0 1
8 2021-07-11 05:00:00 2021-07-11 06:30:00 8.0 1
9 2021-07-11 05:01:00 2021-07-11 06:31:00 9.0 1
10 2021-07-11 05:02:00 2021-07-11 06:32:00 10.0 1
11 2021-07-11 05:03:00 2021-07-11 06:33:00 11.0 1
12 2021-07-11 05:04:00 2021-07-11 06:34:00 12.0 1
...