我正在研究一种预测模型,在这个模型中,我希望使用各种时间集合的数据,如每日、每周、每月、季度和年度。
为了创建预测模型,我使用了库sktime。对于加载数据,sktime与pandas数据格式是兼容的。为了表示时间顺序,sktime允许PeriodIndex对数据进行索引。
对于月度、季度和年度粒度,我在建立索引时没有问题,因为PeriodIndex构造函数有年份、季度、月份和日的参数。但是,它在一周内没有参数。
所以我想知道我该怎么做才能避开这一切?
具体来说,我能够在接下来的时间里得到其他的集合,我本来希望能够在一周内做一些类似的事情,但可惜我做不到。
# 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])
...我想周不是那么简单,因为有些年有52周,而其他有53周。
发布于 2022-10-16 09:19:22
这里有一种方法可以在Pandas 范围的帮助下完成
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]')https://stackoverflow.com/questions/74018293
复制相似问题