I have trained xgboost algorithm to predict the number of items sale on a given day and got pretty good results, now I would like to forecast sales ahead of one week.

I tried re-training the algorithm by adding date as one of the independent variables, however, got an exception on date type.

So can someone please suggest how to predict forecast item sales count next one week.

In-sample predictions code:

xgb = xgboost.XGBRegressor(n_estimators=100, learning_rate=0.08, gamma=0, subsample=0.75,
     colsample_bytree=1, max_depth=7)
predictions = xgb.predict(X_test)
  • $\begingroup$ do you have any code to show? $\endgroup$
    – oW_
    Commented Jul 19, 2019 at 20:40
  • $\begingroup$ @oW_, Nope, have coded for in-sample predictions, need guidance on out-sample predictions. Added in-sample code in question summary $\endgroup$
    – Optimizor
    Commented Jul 20, 2019 at 11:24

1 Answer 1


Yes, you cannot pass in a datetime object as a feature to XGboost Regressor, because it isn't time-series forecasting tool such as ARIMA.

Instead, you create various features from the timeseries object such as year, month, day of month, day of week, number of days until public holiday etc. All of these are of type int. Drop the timeseries column.

When you're passing in the test data, you convert the datetime object into the above features.

  • $\begingroup$ Sid, have done similar kind of trick but with lagged features.It worked for in-sample predictions. Am having trouble in out of sample predictions. $\endgroup$
    – Optimizor
    Commented Jul 21, 2019 at 7:08
  • $\begingroup$ It probably treated it as a categorical feature $\endgroup$
    – Sid
    Commented Jul 21, 2019 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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