emax
emax

Reputation: 7245

How do I loop over months, skipping three at a time?

I have the following array:

dates
array(['2015-07-01T00:00:00.000000000', '2015-08-01T00:00:00.000000000',
       '2015-09-01T00:00:00.000000000', '2015-10-01T00:00:00.000000000',
       '2015-11-01T00:00:00.000000000', '2015-12-01T00:00:00.000000000',
       '2016-01-01T00:00:00.000000000', '2016-02-01T00:00:00.000000000',
       '2016-03-01T00:00:00.000000000', '2016-04-01T00:00:00.000000000',
       '2016-05-01T00:00:00.000000000', '2016-06-01T00:00:00.000000000',
       '2016-07-01T00:00:00.000000000', '2016-08-01T00:00:00.000000000',
       '2016-09-01T00:00:00.000000000', '2016-10-01T00:00:00.000000000',
       '2016-11-01T00:00:00.000000000', '2016-12-01T00:00:00.000000000',
       '2017-01-01T00:00:00.000000000', '2017-02-01T00:00:00.000000000',
       '2017-03-01T00:00:00.000000000', '2017-04-01T00:00:00.000000000',
       '2017-05-01T00:00:00.000000000', '2017-06-01T00:00:00.000000000',
       '2017-07-01T00:00:00.000000000', '2017-08-01T00:00:00.000000000',
       '2017-09-01T00:00:00.000000000', '2017-10-01T00:00:00.000000000',
       '2017-11-01T00:00:00.000000000', '2017-12-01T00:00:00.000000000'],
      dtype='datetime64[ns]')

I would to create a loop every 3 months that the at the first iteration takes dates[0] and dates[3], at the second one dates[3] and dates[6] and so on

This is what I am doing

c1 = 0
c2 = 3
for i,j in enumerate(dates[:-3]):
    v1 = dates[c1]
    v2 = dates[c2]
    c1 = c1 + 3
    c2 = c2 + 3

Is there a more elegant way to do it?

Upvotes: 0

Views: 69

Answers (3)

Alain T.
Alain T.

Reputation: 42133

You can stride through date array using the subscripts directly:

for v1,v2 in zip(dates[::3],dates[3::3]):
    print(v1,v2)

2015-07-01T00:00:00.000000000 2015-10-01T00:00:00.000000000
2015-10-01T00:00:00.000000000 2016-01-01T00:00:00.000000000
2016-01-01T00:00:00.000000000 2016-04-01T00:00:00.000000000
2016-04-01T00:00:00.000000000 2016-07-01T00:00:00.000000000
2016-07-01T00:00:00.000000000 2016-10-01T00:00:00.000000000
2016-10-01T00:00:00.000000000 2017-01-01T00:00:00.000000000
2017-01-01T00:00:00.000000000 2017-04-01T00:00:00.000000000
2017-04-01T00:00:00.000000000 2017-07-01T00:00:00.000000000
2017-07-01T00:00:00.000000000 2017-10-01T00:00:00.000000000

Upvotes: 0

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

You can use numpy.lib.stride_tricks.as_strided to reshape appropriately:

>>> from numpy.lib.stride_tricks import as_strided
>>> as_strided(x, shape=((x.shape[0])//3 - 1, 2), strides=(x.itemsize*3,)*2)
 
array([['2015-07-01T00:00:00.000000000', '2015-10-01T00:00:00.000000000'],
       ['2015-10-01T00:00:00.000000000', '2016-01-01T00:00:00.000000000'],
       ['2016-01-01T00:00:00.000000000', '2016-04-01T00:00:00.000000000'],
       ['2016-04-01T00:00:00.000000000', '2016-07-01T00:00:00.000000000'],
       ['2016-07-01T00:00:00.000000000', '2016-10-01T00:00:00.000000000'],
       ['2016-10-01T00:00:00.000000000', '2017-01-01T00:00:00.000000000'],
       ['2017-01-01T00:00:00.000000000', '2017-04-01T00:00:00.000000000'],
       ['2017-04-01T00:00:00.000000000', '2017-07-01T00:00:00.000000000'],
       ['2017-07-01T00:00:00.000000000', '2017-10-01T00:00:00.000000000']],
      dtype='datetime64[ns]')

Upvotes: 0

Diego Palacios
Diego Palacios

Reputation: 1144

for date1, date2 in zip(dates[:-3:3], dates[3::3]):
    # do stuff

Upvotes: 1

Related Questions