-1

I am new to Python (from Matlab) and having some trouble with a simple task:

How can I create a regular time series from date X to date Y with intervals of Z units?

E.g. from 1st January 2013 to 31st January 2013 every 10 minutes

In Matlab:

t = datenum(2013,1,1):datenum(0,0,0,0,10,0):datenum(2013,12,31);
  • Possible duplicate of [Creating a range of dates in Python](https://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python) – Patrick Artner May 31 '18 at 08:06
  • Please search the forum before posting (Research == step 0 in [how to ask](https://stackoverflow.com/help/how-to-ask) ) - the dupe gives you possible solutions using pandas, generators and list comps - galore. – Patrick Artner May 31 '18 at 08:15
  • Possible duplicate of [Iterating through a range of dates in Python](https://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python) – Mr. T May 31 '18 at 08:41
  • Thank you @Patrick and Mr T for the links. I had looked through several posts and I made several attempts to imitate their answers to work for my problem with no luck. This question is so simple it's almost embarrassing, yet I couldn't find the solution. The answer below by Rakesh is perfect. – matt_python_newbie Jun 01 '18 at 02:17

3 Answers3

0

If you can use pandas then use pd.date_range

Ex:

import pandas as pd
d = pd.date_range(start='2013/1/1', end='2013/12/31', freq="10min")
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

This function will make an interval of anything, similar to range, but working also for non-integers:

def make_series(begin, end, interval):
    x = begin
    while x < end:
        yield x
        x = x + interval

You can then do this:

>>> import datetime
>>> date_x = datetime.datetime(2013,1,1)
>>> date_y = datetime.datetime(2013,12,31)
>>> step = datetime.timedelta(days=50)
>>>
>>> list(make_series(date_x, date_y, step))
[datetime.datetime(2013, 1, 1, 0, 0), datetime.datetime(2013, 2, 20, 0, 0), datetime.datetime(2013, 4, 11, 0, 0), datetime.datetime(2013, 5, 31, 0, 0), datetime
.datetime(2013, 7, 20, 0, 0), datetime.datetime(2013, 9, 8, 0, 0), datetime.datetime(2013, 10, 28, 0, 0), datetime.datetime(2013, 12, 17, 0, 0)]
zvone
  • 18,045
  • 3
  • 49
  • 77
0

I picked a random date range, but here is how you can essentially get it done:

a = pd.Series({'A': 1, 'B': 2, 'C': 3}, pd.date_range('2018-2-6', '2018-3-4', freq='10Min'))
print(a)

You can add a date range when creating the DataFrame. The first argument that you enter into the date_range() method is the start date, and the second argument is automatically the end date. The last argument that you enter in this example is the frequency argument, which you can set to 10 minutes. You can set it to 'H' for 1 hour, or to 'M' for 1 minute intervals as well. Another method that is worth noting however is asfreq() method that will let you edit the frequency or store a copy of another dataframe with a different frequency. Here is an example:

copy = a.asfreq('45Min', method='pad')
print(copy)

That's important if you want to study multiple frequencies. You can just copy your dataframe repeatedly in order to look at various time intervals. Hope that this answer helps.

Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27