 def buildLaggedFeatures(s,lag=2,dropna=True): ''' Builds a new DataFrame to facilitate regressing over all possible lagged features ''' if type(s) is pd.DataFrame: new_dict={} for col_name in s: new_dict[col_name]=s[col_name] # create lagged Series for l in range(1,lag+1): new_dict['%s_lag%d' %(col_name,l)]=s[col_name].shift(l) res=pd.DataFrame(new_dict,index=s.index) elif type(s) is pd.Series: the_range=range(lag+1) res=pd.concat([s.shift(i) for i in the_range],axis=1) res.columns=['lag_%d' %i for i in the_range] else: print 'Only works for DataFrame or Series' return None if dropna: return res.dropna() else: return res