The answer depends on the kind of relationships that you want to represent between the time feature, and the target variable.

If you encode time as numeric, then you are imposing certain restrictions on the model. For a linear regression model, the effect of time is now monotonic, either the target will increase or decrease with time. For decision trees, time values close to each other will be grouped together.

Encoding time as categorical gives the model more flexibility, but in some cases, the model may not have enough data to learn well. One technique that may be useful is to group time values together into some number of sets, and use the set as a categorical attribute.

Some example groupings:

- For month, group into quarters or seasons, depending upon the use case. Eg: Jan-Mar, Apr-Jun, etc.
- For hour-of-day, group into time-of-day buckets: morning, evening, etc,
- For day-of-week, group into weekday, weekend.

Each of the above can also be used directly as a categorical attribute as well, given enough data. Further, groupings can also be discovered by data analysis, to complement a domain knowledge based approach.

I faced the same issue in defining hour of the day (1 to 24) variable in the RF model. If I convert the variable as categorical, VarImp function shows importance value for each hour and it looks very disorganized. I am just wondering that does it necessary to convert 'hour of the day' type numerical variable to categorical? – Mahmudur Rahman – 2018-02-13T02:51:03.833