Generate 10-min interval start and end times in Python

Viewed 842

I have a task where I need to generate 10-min interval between the given time range. For example,

start_time = '2020-09-02 17:33:04.472', end_time = '2020-09-02 20:19:14.859'

Desired Output:

start_times_list = ['2020-09-02 17:30:00', '2020-09-02 17:40:00', '2020-09-02 17:50:00', '2020-09-02 18:00:00', '2020-09-02 18:10:00', '2020-09-02 18:20:00', '2020-09-02 18:30:00', '2020-09-02 18:40:00','2020-09-02 18:50:00', '2020-09-02 19:00:00', '2020-09-02 19:10:00', '2020-09-02 19:20:00', '2020-09-02 19:30:00', '2020-09-02 19:40:00', '2020-09-02 19:50:00', '2020-09-02 20:00:00', '2020-09-02 20:10:00', '2020-09-02 20:20:00']

end_times_list = ['2020-09-02 17:40:00', '2020-09-02 17:50:00', '2020-09-02 18:00:00', '2020-09-02 18:10:00', '2020-09-02 18:20:00', '2020-09-02 18:30:00', '2020-09-02 18:40:00','2020-09-02 18:50:00', '2020-09-02 19:00:00', '2020-09-02 19:10:00', '2020-09-02 19:20:00', '2020-09-02 19:30:00', '2020-09-02 19:40:00', '2020-09-02 19:50:00', '2020-09-02 20:00:00', '2020-09-02 20:10:00', '2020-09-02 20:20:00', '2020-09-02 20:30:00']

I have tried the below method which creates only a date range from start datetime, but not from the floor(min)

import pandas as pd

l = (pd.DataFrame(columns=['NULL'],
                  index=pd.date_range('2020-09-02 17:33:04', '2020-09-02 20:19:14',
                                      freq='10T'))
       .between_time('07:00','22:00')
       .index.strftime('%Y-%m-%d %H:%M:%S')
       .tolist()
)
l

Current Output

['2020-09-02 17:33:04',
 '2020-09-02 17:43:04',
 '2020-09-02 17:53:04',
 '2020-09-02 18:03:04',
 '2020-09-02 18:13:04',
 '2020-09-02 18:23:04',
 '2020-09-02 18:33:04',
 '2020-09-02 18:43:04',
 '2020-09-02 18:53:04',
 '2020-09-02 19:03:04',
 '2020-09-02 19:13:04',
 '2020-09-02 19:23:04',
 '2020-09-02 19:33:04',
 '2020-09-02 19:43:04',
 '2020-09-02 19:53:04',
 '2020-09-02 20:03:04',
 '2020-09-02 20:13:04']
3 Answers

You could use Timestamp.round() to round the start/end times to 10 minutes.

import pandas as pd
from datetime import datetime

start_time = '2020-09-02 17:33:04.472'

end_time = '2020-09-02 20:19:14.859'
  
start_times = pd.date_range(start= pd.Timestamp(start_time).round('10T'), end = pd.Timestamp(end_time).round('10T'), freq="10T")

end_times = start_times + pd.Timedelta('10T')

start_times_list = start_times.strftime('%Y-%m-%d %H:%M:%S').tolist()

end_times_list = end_times.strftime('%Y-%m-%d %H:%M:%S').tolist()

print(start_times_list)
print(end_times_list)

If you are sure about the input format, probably the most easy is to use:

newstarttime = starttime[:-8]+"0:00.000"
import pandas as pd
from datetime import datetime
import math

start_time = '2020-09-02 17:36:04.472'

end_time = '2020-09-02 20:19:14.859'
  
start_times = pd.date_range(start= pd.Timestamp(start_time).floor('10T'), end = pd.Timestamp(end_time).floor('10T'), freq="10T")

end_times = start_times + pd.Timedelta('10T')

start_times_list = start_times.strftime('%Y-%m-%d %H:%M:%S').tolist()

end_times_list = end_times.strftime('%Y-%m-%d %H:%M:%S').tolist()

print(start_times_list)
print(end_times_list)
Related