I just started to learn time series (about time after avoiding it for very long).

I read through some short summaries and jumped straight to see if one can model time series using supervised learning, turns out there's 2 ways.

The way I want to ask is based on the famous Kaggle notebook on XGboost for Time Series. The notebook is clear and one see that he transformed (decomposed) each datetime record to its week, day, month etc to somewhat capture trends/seasonal.

A short pseudo code is below:

def create_features(df, label=None):
    Creates time series features from datetime index
    df['date'] = df.index
    df['hour'] = df['date'].dt.hour
    X = df[['hour','dayofweek','quarter','month','year',
    return ...

Now I tried it myself on a dataset, works fine and predictions made on Unseen Test Set is decent. I thought it's all good and tried to make more features, especially features like rolling mean, rolling median over past timeperiods and soon I ran into a big issue. During training and validation, we have the target values and hence these rolling or lag features can be found. But on unseen test set, there should be no target for us to model...how then should we go about forecasting, say 3 days ahead? Note that I did not notice this because the initial features are agnostic of target, as long as we have a datetime, we can create these features.

I hope someone can guide me to some good tutorials on this. I read the de-facto tutorials here but he is using the other way to model time series as supervised.

  • 1
    $\begingroup$ Hi @nan. I come from a slightly different background with classical time series (TS) learning first, and learning machine learning (ML) later for some side projects. All I would say is that with ML, it is often implied that your data is IID; but TS data is explicitly non-IID. So, you have to ensure your data is suitably transformed to uphold the IID/stationarity type conditions. I would say many ML tutorials overlook these types of preliminaries, and the requirements for out-of-sample TS forecasts/predictions. $\endgroup$
    – EB3112
    Mar 31 at 9:49

1 Answer 1


The rolling mean or lagged target values will always belong to past. If you plan to predict $t+k$ from up to day $t$ features, the rolling mean, however long it is, should contain the mean of the target values until day $t$.

At any arbitrary time $t$ in the test set, you can assume you have the data available until (and including) day $t-k$; even if day $t-k$ is in the test set. You should preprocess your test set accordingly.

  • $\begingroup$ Thanks, I tried and soon ran into a problem, let's say I have a feature called 6-hours lag, then if at time t = 1am, we can forecast 7am since we do have t's value, but we cannot forecast 8am since we do not know 2am's value. I need to be able to forecast more than 1 step ahead, is recursive-multistep a recommended way? $\endgroup$
    – nan
    Apr 1 at 13:42
  • $\begingroup$ If you want to extrapolate, yes, that is certainly an option. This means the problem is not just predicting $k$ time ahead, but you want to predict, say $[t+k, t+m]$ from time $t$. $\endgroup$
    – gunes
    Apr 1 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.