# Engineering lag features for the test set in time-series machine learning

I am trying to do time series forecasting through machine learning. I want to engineer lag features, but was wondering what would be the best way to go about generating these features for the test set (or validation folds). Now, I'm sure that I can't just use test data to engineer these features - should I instead be using the predictions, and generating the lags iteratively?

For example, if I'm only using a 1-period lag, I would use the last entry in my training set as the lag for the first entry in my test set. I make my prediction, then use that as the 1-period lag for the second value in my test set, and so on. Is this the correct way to use lag features with machine learning? And is there a function in sklearn or another library that automates this process?

Say that your test set starts at time point $$t$$ up to time point $$N$$, and you are using time lags of $$1, 2,$$ and $$3$$. In such case, you need data from time points $$t, t+1, t+2$$ to predict for $$t+3$$, all the way up to $$N-3, N-2, N-1$$ to predict for $$N$$. What follows, you cannot make predictions for the first three time points.
As you can see, to achieve this, you need somehow to split the data on time. Simplest solution is to take points $$1,2,\dots,t-1$$ to train set and points $$t,t+1,\dots,N$$ go to test set (assuming it is sorted). Another possibility, is to split the data on time into some number of blocks that are randomly assigned to either train, or test set (e.g. values $$1,2,\dots,10$$ go to train set $$11,12,\dots,20$$ to test set, $$21,...$$ again to train set, etc), you can rotate the blocks for $$k$$-fold cross-validation. Finally, you could use a variation of one-step ahead cross-validation.