Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python generate dates series

How can i generate array with dates like this:

Timestamps in javascript miliseconds format from 2010.12.01 00:00:00 to 2010.12.12.30 23.59.59 with step 5 minutes.

['2010.12.01 00:00:00', '2010.12.01 00:05:00','2010.12.01 00:10:00','2010.12.01 00:15:00', ...] 
like image 580
Rafał Kot Avatar asked Dec 18 '10 19:12

Rafał Kot


People also ask

How do you range a date in Python?

However you can use np. arange(…). astype(dt. datetime) to make arange return native python datetime instead of numpy datetime64.


2 Answers

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) 
like image 113
cantdutchthis Avatar answered Sep 24 '22 18:09

cantdutchthis


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

import datetime  dt = 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.

like image 40
Lennart Regebro Avatar answered Sep 25 '22 18:09

Lennart Regebro