Я раздумывал, стоит ли создайте два столбца для дня недели и номера недели, затем создайте сводную таблицу по годам, а затем каким-то образом сдвиньте годы вперед. Но я выбрал описанный ниже подход (который на самом деле не работает и очень медленный).
Код: Выделить всё
def forecast(row):
# set the year lookup to the previous year, means no values for the first year
year = row.name.year - 1
# match the weekday as not great to compare a working day to a weekend
weekday = row.name.weekday()
# match the week number too, as not great comparing e.g. Feb to May
week = row.name.isocalendar().week
return df.loc[(df.index.year == year) &
(df.index.weekday == weekday) &
(df.index.isocalendar().week == week)]
df['prediction'] = df.apply(lambda row: forecast(row), axis=1)
Код: Выделить всё
Day Value
2012-04-01 9.56
2012-04-02 9.37
2012-04-03 9.72
2012-04-04 11.27
2012-04-05 12.56
...
2022-03-27 10.52
2022-03-28 10.32
2022-03-29 11.19
2022-03-30 12.66
2022-03-31 15.34
Подробнее здесь: https://stackoverflow.com/questions/737 ... -number-fr