Python generate dates series Python generate dates series python python

Python generate dates series


I just felt that it might be worthwhile to note that pandas also has this functionality. Depending on what case you are dealing with exactly, pandas might be a worthy tool to invest time in.

import pandas as pd times = pd.date_range('2012-10-01', periods=289, freq='5min')

This returns a pandas timeseries-index. Which can be converted to numpy arrays.

np.array(times)


Well, obviously you start at the start time, loop until you reach the end time and increment inbetween.

import datetimedt = datetime.datetime(2010, 12, 1)end = datetime.datetime(2010, 12, 30, 23, 59, 59)step = datetime.timedelta(seconds=5)result = []while dt < end:    result.append(dt.strftime('%Y-%m-%d %H:%M:%S'))    dt += step

Fairly trivial.


this is my variant for python3, but it's easy could be converted into python2.6 code:

import datetime as dtdt1 = dt.datetime(2010, 12, 1)dt2 = dt.datetime(2010, 12, 12, 23, 59, 59)time_step = 5 # secoondsdelta = dt2 - dt1delta_sec = delta.days * 24 * 60 * 60 + delta.secondsres = [dt1 + dt.timedelta(0, t) for t in range(0, delta_sec, time_step)]