def zip_timeseries(*series, **kwargs):
 next_slice = (max if (kwargs.get('order', 'descending') == 'descending') else min)
 iterators = [PeekableIterator(s) for s in series]
 widths = []
 for w in iterators:
  r = w.peek()
  if r:
   (date, values) = r
   widths.append(len(values))
  else:
   widths.append(0)
 while True:
  items = [it.peek() for it in iterators]
  if (not any(items)):
   return
  current_slice = next_slice((item[0] for item in items if item))
  data = []
  for (i, item) in enumerate(items):
   if (item and (item[0] == current_slice)):
    data.extend(item[1])
    iterators[i].next()
   else:
    data.extend(([0] * widths[i]))
  (yield (current_slice, tuple(data)))

def zip_timeseries ( * series , ** kwargs ) : next_slice = ( max if ( kwargs . get ( 'order' , 'descending' ) == 'descending' ) else min ) iterators = [ PeekableIterator ( s ) for s in series ] widths = [ ] for w in iterators : r = w . peek ( ) if r : ( date , values ) = r widths . append ( len ( values ) ) else : widths . append ( 0 ) while True : items = [ it . peek ( ) for it in iterators ] if ( not any ( items ) ) : return current_slice = next_slice ( ( item [ 0 ] for item in items if item ) ) data = [ ] for ( i , item ) in enumerate ( items ) : if ( item and ( item [ 0 ] == current_slice ) ) : data . extend ( item [ 1 ] ) iterators [ i ] . next ( ) else : data . extend ( ( [ 0 ] * widths [ i ] ) ) ( yield ( current_slice , tuple ( data ) ) )
zip timeseries data while gracefully handling gaps in the data .