0

I want to identify groups of continuos dates (no time gaps) in datetime index in a similar way to this question

so let's say I create a list of datetime with gaps

ref=pd.date_range(start='2014-01-01 08:00:00',end='2014-01-01 09:30:00',freq='10 min')
ref=ref.delete([3,4])

'''how should I define my lambda function in order to iterate on consecutive datetime elements'''

ranges = []
for k, g in groupby(enumerate(ref), lambda (i,x):i-x):
    group = map(itemgetter(1), g)
    ranges.append((group[0], group[-1]))

So the result should be something like

[('2014-01-01 08:00:00','2014-01-01 08:20:00'),('2014-01-01 08:50:00',
               '2014-01-01 09:30:00')]

I know how to do it with list comprehension and for loops but I was looking for something more efficient like this itertools

Community
  • 1
  • 1
gis20
  • 1,024
  • 2
  • 15
  • 33

1 Answers1

0

So finally I got something

groups=[list(g) for k, g in groupby(izip(ref,ref[1:]), lambda (i,x):x-i) if k==pd.Timedelta('10 min').to_timedelta64()]
#groups includes the groups of elements continuos. No 10 min gaps
ranges=[(i[0][0],i[-1][-1])for i in groups]
#ranges, the first and final value
gis20
  • 1,024
  • 2
  • 15
  • 33