2

I am working on a forecasting model in which I want to use various temporal aggregates of data such as daily, weekly, monthly, quarterly, and yearly.

For creating the forecast models, I am using the library sktime. For loading the data sktime is compatible with pandas dataframes. In order to denote the temporal ordering, sktime allows the dataframe to be indexed by PeriodIndex.

For monthly, quarterly, and yearly granularities I do not have a problem setting up the index since the PeriodIndex constructor has parameters for year, quarter, month, and day. However, it does not have a parameter for week.

So I am wondering what I am supposed to do to get around this?

To be specific, I am able to get the other aggregates in the following away, and I would have expected to be able to do something similar for week, but alas I cannot.

# DAILY
dates = ["01/01/2000", "02/01/2000", "25/01/2000", "01/01/3005"]
pd.PeriodIndex(dates, freq='d')

# MONTHLY
pd.PeriodIndex(year=[2000]*12, month=[1,2,3,4,5,6,7,8,9,10,11,12], freq='w')

# QUARTERLY
pd.PeriodIndex(year=[2000,2000,2000,2000], quarter=[1,2,3,4])

...

I assume week is not as straightforward since some years have 52 weeks and others 53.

Laurent
  • 12,287
  • 7
  • 21
  • 37
Luca Guarro
  • 1,085
  • 1
  • 11
  • 25

1 Answers1

0

Here is one way to do it with the help of Pandas date_range:

import pandas as pd

idx = pd.PeriodIndex(
    pd.date_range("01/01/2000", "31/12/2000", freq="w"), freq="W-MON"
)

print(idx)
# Output
PeriodIndex(['1999-12-28/2000-01-03', '2000-01-04/2000-01-10',
             '2000-01-11/2000-01-17', '2000-01-18/2000-01-24',
             '2000-01-25/2000-01-31', '2000-02-01/2000-02-07',
             '2000-02-08/2000-02-14', '2000-02-15/2000-02-21',
             '2000-02-22/2000-02-28', '2000-02-29/2000-03-06',
             '2000-03-07/2000-03-13', '2000-03-14/2000-03-20',
             '2000-03-21/2000-03-27', '2000-03-28/2000-04-03',
             '2000-04-04/2000-04-10', '2000-04-11/2000-04-17',
             '2000-04-18/2000-04-24', '2000-04-25/2000-05-01',
             '2000-05-02/2000-05-08', '2000-05-09/2000-05-15',
             '2000-05-16/2000-05-22', '2000-05-23/2000-05-29',
             '2000-05-30/2000-06-05', '2000-06-06/2000-06-12',
             '2000-06-13/2000-06-19', '2000-06-20/2000-06-26',
             '2000-06-27/2000-07-03', '2000-07-04/2000-07-10',
             '2000-07-11/2000-07-17', '2000-07-18/2000-07-24',
             '2000-07-25/2000-07-31', '2000-08-01/2000-08-07',
             '2000-08-08/2000-08-14', '2000-08-15/2000-08-21',
             '2000-08-22/2000-08-28', '2000-08-29/2000-09-04',
             '2000-09-05/2000-09-11', '2000-09-12/2000-09-18',
             '2000-09-19/2000-09-25', '2000-09-26/2000-10-02',
             '2000-10-03/2000-10-09', '2000-10-10/2000-10-16',
             '2000-10-17/2000-10-23', '2000-10-24/2000-10-30',
             '2000-10-31/2000-11-06', '2000-11-07/2000-11-13',
             '2000-11-14/2000-11-20', '2000-11-21/2000-11-27',
             '2000-11-28/2000-12-04', '2000-12-05/2000-12-11',
             '2000-12-12/2000-12-18', '2000-12-19/2000-12-25',
             '2000-12-26/2001-01-01'],
            dtype='period[W-MON]')
Laurent
  • 12,287
  • 7
  • 21
  • 37