def lagged_groups(x, lag, groupidx):
 out0 = []
 out_lagged = []
 for (l, u) in groupidx:
  if ((l + lag) < u):
   out0.append(x[(l + lag):u])
   out_lagged.append(x[l:(u - lag)])
 if (out0 == []):
  raise ValueError('all groups are empty taking lags')
 return (np.vstack(out0), np.vstack(out_lagged))

def lagged_groups ( x , lag , groupidx ) : out0 = [ ] out_lagged = [ ] for ( l , u ) in groupidx : if ( ( l + lag ) < u ) : out0 . append ( x [ ( l + lag ) : u ] ) out_lagged . append ( x [ l : ( u - lag ) ] ) if ( out0 == [ ] ) : raise ValueError ( 'all groups are empty taking lags' ) return ( np . vstack ( out0 ) , np . vstack ( out_lagged ) )
assumes sorted by time .