1

What could be a good way to generate timestamp series for 5 minute interval with a start time of say 09:10:00 and end time of 15:30:00

I know I could hard-code it like below (with all entries, but there must be a clean way where I can just give interval.

times=[pd.to_datetime(i).time() for i in '10:15:00','10:15:05','10:15:10','10:15:15','10:15:20','15:25:00','15:30:00']

I tried

datetime_range(datetime.time(09,15,00), datetime.time(15,30,00),  timedelta(minutes=5))

But this gives SyntaxError: invalid token

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
pythonRcpp
  • 2,042
  • 6
  • 26
  • 48
  • `datetime.time(09,15,00)` must be `datetime.time(9,15,00)`. Python interprets 09 is an attempt to define an octal number (octal numbers start with a 0), but 9 is not an octal digit. – DYZ Jul 18 '17 at 06:08
  • I tried `datetime_range(datetime.time(9,15,00), datetime.time(15,30,00), timedelta(minutes=15))` but it says `descriptor 'time' requires a 'datetime.datetime' object but received a 'int'` – pythonRcpp Jul 18 '17 at 06:10
  • 1
    @DYZ This is a Python 3 specific feature, see https://stackoverflow.com/questions/13013638/python-cannot-handle-numbers-string-starting-with-0-why 2 **is** an octal digit but in Python 3 `02` still raises "Invalid token". – AGN Gazer Jul 18 '17 at 06:14
  • What is `datetime_range()`? – DYZ Jul 18 '17 at 06:15

2 Answers2

3

How about this?

times = [(datetime.datetime(2017, 7, 17, 9, 10, 0) + datetime.timedelta(minutes=5*x)).time() for x in range(5)]

It's a little long, but it all fits on one line like it looks like you want to do. Of course you can work around that with importing the functions you need instead of the whole module.

Cory Madden
  • 5,026
  • 24
  • 37
  • yes this is what I want. But since I am writing this to a script, I cannot give date in this form ("hardcoded"). – pythonRcpp Jul 18 '17 at 06:28
  • I don't understand. The example in your code shows you formatting it this way. Whatever it is, you should be able to convert it to a datetime object and then continue with the above list comprehension. – Cory Madden Jul 18 '17 at 06:49
  • All I was saying was that including 2017,07,17 (date) doesnt look ok inside a script. Although it does not make any difference since we are taking the `time() ` only part of it. – pythonRcpp Jul 18 '17 at 06:51
  • Im trying to do `datetime.time(10, 15) + datetime.timedelta(minutes=5)` but getting `TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'` can you tell what is wrong in this – pythonRcpp Jul 18 '17 at 09:39
  • Sorry, just woke up. The problem is that you can't use `time` objects with `timedelta` which is why you need to convert the `time` to a `datetime` which can be done a number of ways, but it would be easier to Google that question. – Cory Madden Jul 18 '17 at 14:56
0

What could be a good way to generate timestamp series for 5 minute interval with a start time of say 09:10:00 and end time of 15:30:00

A simple loop should do the trick:

from datetime import datetime, time, timedelta

times = []
ts = datetime(2017, 7, 17, 9, 10, 0)
while ts <= datetime(2017, 7, 17, 15, 30, 0):
    times.append(time(ts.hour, ts.minute, ts.second))
    ts += timedelta(minutes=5)

The reason for using datetime objects to start with is that they support timedelta objects which make short work of your problem. From there, it is easy to convert to a time object with time(ts.hour, ts.minute, ts.second).

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485