How do I use number of hours as index in timeseries forecasting?

I have a dataset that has number of hours (consecutive value) and total sales in that 1 hour in my dataset. See below for head of the dataset:

t    sales
--------------
23   172.3676
24   176.3456
25   166.9039
26   153.9990
27   167.9585

I want to forecast the sales for the next 10 hours. I also set column t as the index. However, when I try to get the seasonal decomposition, it shows an error:

result = seasonal_decompose(train['sales'].dropna(), model='additive', freq =12)
result.plot()
plt.show()

TypeError:seasonal_decompose() got an unexpected keyword argument 'freq'

How do I handle the number of hours in a time series model? Do I need to convert it to a different format before using it as the index? Also, the sales column is continuous numeric value, do I need to round it off?

Thanks in advance!

Topic forecasting statsmodels time-series python

Category Data Science


  1. The frequency parameter of statsmodels’ seasonal_decompose() method has been deprecated and replaced with the period parameter. Please use period in place of frequency.

  2. Since the data you provided is hourly, the period should be 24. The period determines how often the cycle repeats in the seasonal component. For example, with monthly data, the period would usually be 12. With hourly, it could be 24 (daily) or 168 (weekly). This is something that you should know about your data. Or you can try alternative plausible values when doing EDA (other examples 7-daily, 12-monthly, 52-weekly).

  3. The date should be in datetime format and need to be set as index using .set_index(), e.g., train.set_index('Date', inplace=True). In your case Date is t (as per your dataset).

  4. Also the code should work fine for continuous numeric value. It worked for me while working on daily stock price data.

I think below code should work fine for you, but make sure 3rd step is followed.

result = seasonal_decompose(train['sales'].dropna(), model='additive', period=24)
result.plot()
plt.show()

Reference:

https://github.com/statsmodels/statsmodels/issues/3503

https://towardsdatascience.com/time-series-decomposition-and-statsmodels-parameters-69e54d035453

Waiting for your feedback.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.