I was trying to replicate this code for stat forecasting in python, I came across an odd error "name 'forecasts' is not defined" which is quite strange as I was able to replicate the code without any errors before.
I believe this was resolved in the latest update of this library StatsForecast but I still run across to the same error. Can you please help me out here.
The code I am replicating is from this : https://towardsdatascience.com/time-series-forecasting-with-statistical-models-f08dcd1d24d1
A similar question was earlier asked for the same error, and the solution was updated but this error till comes up after the new solution as well, attached is the link to the question: Error in Data frame definition while Multiple TS Stat Forecasting in Python
import random
from itertools import product
from IPython.display import display, Markdown
from multiprocessing import cpu_count
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from statsforecast import StatsForecast
from nixtlats.data.datasets.m4 import M4, M4Info
from statsforecast.models import CrostonClassic, CrostonSBA, CrostonOptimized, ADIDA, IMAPA, TSB
from statsforecast.models import SimpleExponentialSmoothing, SimpleExponentialSmoothingOptimized, SeasonalExponentialSmoothing, SeasonalExponentialSmoothingOptimized, Holt, HoltWinters
from statsforecast.models import HistoricAverage, Naive, RandomWalkWithDrift, SeasonalNaive, WindowAverage, SeasonalWindowAverage
from statsforecast.models import MSTL
from statsforecast.models import Theta, OptimizedTheta, DynamicTheta, DynamicOptimizedTheta
from statsforecast.models import AutoARIMA, AutoETS, AutoCES, AutoTheta
df = pd.read_excel ('C:/X/X/X/Data.xlsx',sheet_name='Transpose')
df.rename(columns = {'Row Labels':'Key'}, inplace=True)
df['Key'] = df['Key'].astype(str)
df = pd.melt(df,id_vars='Key',value_vars=list(df.columns[1:]),var_name ='ds')
df.columns = df.columns.str.replace('Key', 'unique_id')
df.columns = df.columns.str.replace('value', 'y')
df["ds"] = pd.to_datetime(df["ds"],format='%Y-%m-%d')
df=df[["ds","unique_id","y"]]
df['unique_id'] = df['unique_id'].astype('object')
df = df.set_index('unique_id')
df.reset_index()
seasonality = 30 #Monthly data
models = [
ADIDA,
CrostonClassic(),
CrostonSBA(),
CrostonOptimized(),
IMAPA,
(TSB,0.3,0.2),
MSTL,
Theta,
OptimizedTheta,
DynamicTheta,
DynamicOptimizedTheta,
AutoARIMA,
AutoETS,
AutoCES,
AutoTheta,
HistoricAverage,
Naive,
RandomWalkWithDrift,
(SeasonalNaive, seasonality),
(SeasonalExponentialSmoothing, seasonality, 0.2),
(SeasonalWindowAverage, seasonality, 2 * seasonality),
(WindowAverage, 2 * seasonality)
]
fcst = StatsForecast(df=df, models=models, freq='MS', n_jobs=cpu_count())
%time forecasts = fcst.forecast(9)
forecasts.reset_index()
forecasts = forecasts.round(0)
forecasts.to_excel("C:/X/X/X/Forecast_Output.xlsx",sheet_name='Sheet1')
The dataset I am working with is given below:
{'Row Labels': {0: 'XYZ-912750', 1: 'XYZ-461356', 2: 'XYZ-150591', 3: 'XYZ-627885', 4: 'XYZ-582638', 5: 'XYZ-631691', 6: 'XYZ-409952', 7: 'XYZ-245245', 8: 'XYZ-230662', 9: 'XYZ-533388', 10: 'XYZ-248225', 11: 'XYZ-582912', 12: 'XYZ-486079', 13: 'XYZ-867685', 14: 'XYZ-873555', 15: 'XYZ-375397', 16: 'XYZ-428066', 17: 'XYZ-774244', 18: 'XYZ-602796', 19: 'XYZ-267306', 20: 'XYZ-576156', 21: 'XYZ-775994', 22: 'XYZ-226742', 23: 'XYZ-641711', 24: 'XYZ-928543', 25: 'XYZ-217200', 26: 'XYZ-971921', 27: 'XYZ-141388', 28: 'XYZ-848360', 29: 'XYZ-864999', 30: 'XYZ-821384', 31: 'XYZ-516339', 32: 'XYZ-462488', 33: 'XYZ-140964', 34: 'XYZ-225559', 35: 'XYZ-916534', 36: 'XYZ-389683', 37: 'XYZ-247803', 38: 'XYZ-718639', 39: 'XYZ-512944', 40: 'XYZ-727601', 41: 'XYZ-315757', 42: 'XYZ-764867', 43: 'XYZ-918344', 44: 'XYZ-430939', 45: 'XYZ-204784', 46: 'XYZ-415285', 47: 'XYZ-272089', 48: 'XYZ-812045', 49: 'XYZ-889257', 50: 'XYZ-275863', 51: 'XYZ-519930', 52: 'XYZ-102141', 53: 'XYZ-324473', 54: 'XYZ-999148', 55: 'XYZ-514915', 56: 'XYZ-932751', 57: 'XYZ-669878', 58: 'XYZ-233459', 59: 'XYZ-289984', 60: 'XYZ-150061', 61: 'XYZ-355028', 62: 'XYZ-881803', 63: 'XYZ-721426', 64: 'XYZ-522174', 65: 'XYZ-790172', 66: 'XYZ-744677', 67: 'XYZ-617017', 68: 'XYZ-982812', 69: 'XYZ-940695', 70: 'XYZ-119041', 71: 'XYZ-313844', 72: 'XYZ-868117', 73: 'XYZ-791717', 74: 'XYZ-100742', 75: 'XYZ-259687', 76: 'XYZ-688842', 77: 'XYZ-247326', 78: 'XYZ-360939', 79: 'XYZ-185017', 80: 'XYZ-244773', 81: 'XYZ-289058', 82: 'XYZ-477846', 83: 'XYZ-305072', 84: 'XYZ-828236', 85: 'XYZ-668927', 86: 'XYZ-616913', 87: 'XYZ-874876', 88: 'XYZ-371693', 89: 'XYZ-951238', 90: 'XYZ-371675', 91: 'XYZ-736997', 92: 'XYZ-922244', 93: 'XYZ-883225', 94: 'XYZ-267555', 95: 'XYZ-704013', 96: 'XYZ-874917', 97: 'XYZ-567402', 98: 'XYZ-167338', 99: 'XYZ-592671', 100: 'XYZ-130168', 101: 'XYZ-492522', 102: 'XYZ-696211', 103: 'XYZ-310469', 104: 'XYZ-973277', 105: 'XYZ-841356', 106: 'XYZ-389440', 107: 'XYZ-613876', 108: 'XYZ-662850', 109: 'XYZ-800625', 110: 'XYZ-500125', 111: 'XYZ-539949', 112: 'XYZ-576121', 113: 'XYZ-339006', 114: 'XYZ-247314', 115: 'XYZ-129049', 116: 'XYZ-980653', 117: 'XYZ-678520', 118: 'XYZ-584841', 119: 'XYZ-396755', 120: 'XYZ-409502', 121: 'XYZ-824561', 122: 'XYZ-825996', 123: 'XYZ-820540', 124: 'XYZ-264710', 125: 'XYZ-241176', 126: 'XYZ-491386', 127: 'XYZ-914132', 128: 'XYZ-496194', 129: 'XYZ-941615', 130: 'XYZ-765328', 131: 'XYZ-540602', 132: 'XYZ-222660', 133: 'XYZ-324367', 134: 'XYZ-583764', 135: 'XYZ-248478', 136: 'XYZ-379180', 137: 'XYZ-628462', 138: 'XYZ-454262'}, '2021-03-01': {0: 0, 1: 951, 2: 0, 3: 0, 4: 13, 5: 0, 6: 0, 7: 0, 8: 487, 9: 501, 10: 0, 11: 0, 12: 0, 13: 0, 14: 715, 15: 726, 16: 235, 17: 340, 18: 0, 19: 0, 20: 0, 21: 960, 22: 127, 23: 92, 24: 0, 25: 0, 26: 170, 27: 0, 28: 0, 29: 0, 30: 0, 31: 133, 32: 0, 33: 0, 34: 105, 35: 168, 36: 0, 37: 500, 38: 0, 39: 0, 40: 61, 41: 0, 42: 212, 43: 101, 44: 0, 45: 0, 46: 0, 47: 83, 48: 185, 49: 0, 50: 131, 51: 67, 52: 0, 53: 141, 54: 0, 55: 140, 56: 0, 57: 0, 58: 180, 59: 0, 60: 0, 61: 99, 62: 63, 63: 0, 64: 0, 65: 1590, 66: 0, 67: 0, 68: 15, 69: 113, 70: 0, 71: 0, 72: 0, 73: 54, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 108, 80: 0, 81: 62, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 29, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 69, 98: 0, 99: 0, 100: 0, 101: 62, 102: 30, 103: 42, 104: 0, 105: 0, 106: 0, 107: 67, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 52, 116: 36, 117: 0, 118: 110, 119: 0, 120: 44, 121: 0, 122: 102, 123: 0, 124: 71, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 77, 132: 56, 133: 0, 134: 0, 135: 103, 136: 0, 137: 0, 138: 53}, '2021-04-01': {0: 0, 1: 553, 2: 0, 3: 0, 4: 18, 5: 0, 6: 0, 7: 0, 8: 313, 9: 1100, 10: 0, 11: 0, 12: 0, 13: 0, 14: 336, 15: 856, 16: 216, 17: 415, 18: 0, 19: 0, 20: 0, 21: 1363, 22: 148, 23: 171, 24: 0, 25: 0, 26: 260, 27: 0, 28: 0, 29: 0, 30: 0, 31: 229, 32: 0, 33: 0, 34: 286, 35: 215, 36: 0, 37: 381, 38: 0, 39: 0, 40: 171, 41: 0, 42: 261, 43: 211, 44: 0, 45: 0, 46: 0, 47: 94, 48: 167, 49: 0, 50: 171, 51: 111, 52: 0, 53: 229, 54: 0, 55: 104, 56: 0, 57: 0, 58: 158, 59: 0, 60: 0, 61: 142, 62: 156, 63: 0, 64: 0, 65: 1152, 66: 0, 67: 0, 68: 19, 69: 160, 70: 0, 71: 0, 72: 0, 73: 50, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 146, 80: 0, 81: 25, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 69, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 49, 98: 0, 99: 0, 100: 0, 101: 22, 102: 46, 103: 48, 104: 0, 105: 0, 106: 0, 107: 60, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 24, 116: 51, 117: 0, 118: 112, 119: 0, 120: 73, 121: 0, 122: 155, 123: 0, 124: 57, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 59, 132: 62, 133: 0, 134: 0, 135: 132, 136: 0, 137: 0, 138: 70}, '2021-05-01': {0: 0, 1: 439, 2: 0, 3: 0, 4: 13, 5: 0, 6: 0, 7: 0, 8: 119, 9: 735, 10: 0, 11: 0, 12: 0, 13: 0, 14: 183, 15: 70, 16: 79, 17: 244, 18: 0, 19: 0, 20: 0, 21: 2842, 22: 30, 23: 76, 24: 0, 25: 0, 26: 95, 27: 0, 28: 0, 29: 0, 30: 0, 31: 38, 32: 0, 33: 0, 34: 197, 35: 114, 36: 0, 37: 140, 38: 0, 39: 0, 40: 91, 41: 0, 42: 82, 43: 83, 44: 0, 45: 0, 46: 0, 47: 35, 48: 126, 49: 0, 50: 83, 51: 101, 52: 0, 53: 94, 54: 0, 55: 100, 56: 0, 57: 0, 58: 89, 59: 0, 60: 0, 61: 94, 62: 112, 63: 0, 64: 0, 65: 1903, 66: 0, 67: 0, 68: 61, 69: 91, 70: 0, 71: 0, 72: 0, 73: 30, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 116, 80: 0, 81: 12, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 56, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 0, 98: 0, 99: 0, 100: 0, 101: 20, 102: 42, 103: 35, 104: 0, 105: 0, 106: 0, 107: 59, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 0, 116: 27, 117: 0, 118: 45, 119: 0, 120: 49, 121: 0, 122: 129, 123: 0, 124: 58, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 41, 132: 41, 133: 0, 134: 0, 135: 61, 136: 0, 137: 0, 138: 38}, '2021-06-01': {0: 0, 1: 390, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 221, 9: 816, 10: 0, 11: 0, 12: 0, 13: 0, 14: 109, 15: 255, 16: 126, 17: 161, 18: 0, 19: 0, 20: 0, 21: 959, 22: 52, 23: 119, 24: 0, 25: 0, 26: 261, 27: 0, 28: 0, 29: 0, 30: 0, 31: 142, 32: 0, 33: 0, 34: 203, 35: 42, 36: 0, 37: 133, 38: 0, 39: 0, 40: 113, 41: 0, 42: 118, 43: 62, 44: 0, 45: 0, 46: 0, 47: 48, 48: 112, 49: 0, 50: 75, 51: 105, 52: 0, 53: 107, 54: 0, 55: 102, 56: 0, 57: 0, 58: 77, 59: 0, 60: 0, 61: 81, 62: 94, 63: 0, 64: 0, 65: 764, 66: 0, 67: 0, 68: 47, 69: 116, 70: 0, 71: 0, 72: 0, 73: 19, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 148, 80: 0, 81: 20, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 46, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 33, 98: 0, 99: 0, 100: 0, 101: 39, 102: 52, 103: 47, 104: 0, 105: 0, 106: 0, 107: 56, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 62, 116: 41, 117: 0, 118: 51, 119: 0, 120: 59, 121: 0, 122: 73, 123: 0, 124: 34, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 17, 132: 42, 133: 0, 134: 0, 135: 74, 136: 0, 137: 0, 138: 58}, '2021-07-01': {0: 0, 1: 349, 2: 0, 3: 0, 4: 11, 5: 0, 6: 0, 7: 0, 8: 222, 9: 418, 10: 0, 11: 0, 12: 0, 13: 0, 14: 104, 15: 57, 16: 92, 17: 118, 18: 0, 19: 0, 20: 0, 21: 2040, 22: 80, 23: 50, 24: 0, 25: 0, 26: 147, 27: 0, 28: 0, 29: 0, 30: 0, 31: 22, 32: 0, 33: 0, 34: 117, 35: 88, 36: 0, 37: 146, 38: 0, 39: 0, 40: 65, 41: 0, 42: 117, 43: 65, 44: 0, 45: 0, 46: 0, 47: 33, 48: 36, 49: 0, 50: 51, 51: 50, 52: 0, 53: 66, 54: 0, 55: 51, 56: 0, 57: 0, 58: 100, 59: 0, 60: 0, 61: 63, 62: 55, 63: 0, 64: 0, 65: 847, 66: 0, 67: 0, 68: 32, 69: 68, 70: 0, 71: 0, 72: 0, 73: 42, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 72, 80: 0, 81: 27, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 47, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 36, 98: 0, 99: 0, 100: 0, 101: 25, 102: 29, 103: 39, 104: 0, 105: 0, 106: 0, 107: 40, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 37, 116: 41, 117: 0, 118: 29, 119: 0, 120: 54, 121: 0, 122: 75, 123: 0, 124: 41, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 12, 132: 28, 133: 0, 134: 0, 135: 46, 136: 0, 137: 0, 138: 24}, '2021-08-01': {0: 0, 1: 402, 2: 0, 3: 0, 4: 14, 5: 0, 6: 0, 7: 0, 8: 138, 9: 373, 10: 0, 11: 0, 12: 0, 13: 0, 14: 133, 15: 107, 16: 69, 17: 116, 18: 0, 19: 0, 20: 0, 21: 1554, 22: 80, 23: 65, 24: 0, 25: 0, 26: 123, 27: 0, 28: 0, 29: 0, 30: 0, 31: 23, 32: 0, 33: 0, 34: 95, 35: 49, 36: 0, 37: 146, 38: 0, 39: 0, 40: 50, 41: 0, 42: 90, 43: 57, 44: 0, 45: 0, 46: 0, 47: 19, 48: 46, 49: 0, 50: 38, 51: 20, 52: 0, 53: 91, 54: 0, 55: 69, 56: 0, 57: 0, 58: 57, 59: 0, 60: 0, 61: 53, 62: 48, 63: 0, 64: 0, 65: 934, 66: 0, 67: 0, 68: 19, 69: 66, 70: 0, 71: 0, 72: 0, 73: 75, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 86, 80: 0, 81: 33, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 32, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 46, 98: 0, 99: 0, 100: 0, 101: 22, 102: 31, 103: 63, 104: 0, 105: 0, 106: 0, 107: 41, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 42, 116: 42, 117: 0, 118: 30, 119: 0, 120: 32, 121: 0, 122: 70, 123: 0, 124: 40, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 12, 132: 21, 133: 0, 134: 0, 135: 83, 136: 0, 137: 0, 138: 20}, '2021-09-01': {0: 0, 1: 560, 2: 55, 3: 496, 4: 11, 5: 0, 6: 0, 7: 0, 8: 77, 9: 309, 10: 45, 11: 257, 12: 0, 13: 0, 14: 87, 15: 179, 16: 61, 17: 79, 18: 65, 19: 144, 20: 307, 21: 840, 22: 52, 23: 41, 24: 108, 25: 156, 26: 113, 27: 0, 28: 30, 29: 27, 30: 0, 31: 59, 32: 0, 33: 0, 34: 66, 35: 53, 36: 70, 37: 42, 38: 0, 39: 26, 40: 38, 41: 0, 42: 50, 43: 11, 44: 209, 45: 56, 46: 52, 47: 18, 48: 47, 49: 0, 50: 58, 51: 32, 52: 0, 53: 76, 54: 0, 55: 45, 56: 0, 57: 63, 58: 95, 59: 0, 60: 0, 61: 33, 62: 45, 63: 0, 64: 96, 65: 249, 66: 0, 67: 0, 68: 0, 69: 73, 70: 0, 71: 30, 72: 0, 73: 41, 74: 0, 75: 0, 76: 37, 77: 22, 78: 0, 79: 68, 80: 18, 81: 47, 82: 0, 83: 0, 84: 0, 85: 79, 86: 0, 87: 75, 88: 40, 89: 39, 90: 35, 91: 0, 92: 0, 93: 0, 94: 40, 95: 0, 96: 0, 97: 44, 98: 30, 99: 46, 100: 0, 101: 33, 102: 40, 103: 31, 104: 0, 105: 17, 106: 15, 107: 32, 108: 15, 109: 0, 110: 58, 111: 63, 112: 0, 113: 0, 114: 0, 115: 42, 116: 35, 117: 19, 118: 55, 119: 0, 120: 25, 121: 0, 122: 47, 123: 0, 124: 37, 125: 16, 126: 24, 127: 124, 128: 67, 129: 0, 130: 0, 131: 28, 132: 20, 133: 0, 134: 0, 135: 34, 136: 0, 137: 26, 138: 28}, '2021-10-01': {0: 122, 1: 720, 2: 129, 3: 1135, 4: 11, 5: 0, 6: 0, 7: 85, 8: 122, 9: 280, 10: 100, 11: 159, 12: 0, 13: 0, 14: 87, 15: 115, 16: 40, 17: 32, 18: 236, 19: 176, 20: 322, 21: 334, 22: 113, 23: 49, 24: 133, 25: 119, 26: 136, 27: 0, 28: 74, 29: 56, 30: 38, 31: 83, 32: 0, 33: 0, 34: 65, 35: 88, 36: 75, 37: 68, 38: 52, 39: 36, 40: 44, 41: 11, 42: 40, 43: 13, 44: 198, 45: 244, 46: 130, 47: 23, 48: 44, 49: 0, 50: 62, 51: 49, 52: 0, 53: 92, 54: 0, 55: 14, 56: 0, 57: 83, 58: 58, 59: 0, 60: 0, 61: 44, 62: 42, 63: 39, 64: 37, 65: 132, 66: 0, 67: 0, 68: 49, 69: 57, 70: 0, 71: 40, 72: 112, 73: 28, 74: 102, 75: 0, 76: 56, 77: 17, 78: 22, 79: 37, 80: 48, 81: 0, 82: 14, 83: 13, 84: 48, 85: 84, 86: 0, 87: 104, 88: 81, 89: 34, 90: 49, 91: 0, 92: 0, 93: 42, 94: 101, 95: 41, 96: 11, 97: 74, 98: 35, 99: 45, 100: 73, 101: 19, 102: 38, 103: 26, 104: 0, 105: 26, 106: 26, 107: 43, 108: 93, 109: 0, 110: 74, 111: 70, 112: 35, 113: 25, 114: 0, 115: 55, 116: 28, 117: 0, 118: 58, 119: 0, 120: 26, 121: 0, 122: 13, 123: 0, 124: 50, 125: 16, 126: 39, 127: 74, 128: 42, 129: 29, 130: 0, 131: 24, 132: 26, 133: 0, 134: 0, 135: 125, 136: 0, 137: 37, 138: 20}, '2021-11-01': {0: 1331, 1: 1810, 2: 274, 3: 899, 4: 0, 5: 0, 6: 30, 7: 606, 8: 138, 9: 1735, 10: 209, 11: 468, 12: 0, 13: 0, 14: 327, 15: 1394, 16: 73, 17: 187, 18: 1259, 19: 355, 20: 374, 21: 2079, 22: 500, 23: 168, 24: 305, 25: 80, 26: 256, 27: 0, 28: 340, 29: 143, 30: 380, 31: 273, 32: 79, 33: 0, 34: 143, 35: 137, 36: 200, 37: 336, 38: 166, 39: 235, 40: 97, 41: 202, 42: 75, 43: 130, 44: 650, 45: 675, 46: 326, 47: 46, 48: 105, 49: 0, 50: 195, 51: 135, 52: 93, 53: 229, 54: 0, 55: 93, 56: 0, 57: 188, 58: 89, 59: 46, 60: 123, 61: 101, 62: 89, 63: 64, 64: 208, 65: 325, 66: 0, 67: 0, 68: 211, 69: 90, 70: 0, 71: 111, 72: 218, 73: 42, 74: 139, 75: 16, 76: 94, 77: 148, 78: 45, 79: 92, 80: 100, 81: 16, 82: 31, 83: 123, 84: 87, 85: 142, 86: 0, 87: 444, 88: 123, 89: 105, 90: 63, 91: 0, 92: 16, 93: 149, 94: 240, 95: 114, 96: 99, 97: 128, 98: 128, 99: 104, 100: 196, 101: 32, 102: 41, 103: 55, 104: 0, 105: 67, 106: 97, 107: 56, 108: 40, 109: 14, 110: 194, 111: 290, 112: 151, 113: 154, 114: 11, 115: 105, 116: 54, 117: 30, 118: 148, 119: 0, 120: 71, 121: 0, 122: 39, 123: 0, 124: 118, 125: 207, 126: 58, 127: 131, 128: 93, 129: 30, 130: 0, 131: 90, 132: 43, 133: 0, 134: 0, 135: 40, 136: 0, 137: 58, 138: 29}, '2021-12-01': {0: 1901, 1: 2469, 2: 298, 3: 1760, 4: 14, 5: 0, 6: 573, 7: 1444, 8: 126, 9: 1568, 10: 220, 11: 497, 12: 0, 13: 71, 14: 248, 15: 1670, 16: 77, 17: 93, 18: 910, 19: 362, 20: 698, 21: 1044, 22: 651, 23: 156, 24: 208, 25: 185, 26: 314, 27: 0, 28: 356, 29: 205, 30: 570, 31: 186, 32: 25, 33: 0, 34: 117, 35: 90, 36: 385, 37: 228, 38: 410, 39: 270, 40: 63, 41: 228, 42: 50, 43: 53, 44: 450, 45: 896, 46: 431, 47: 74, 48: 62, 49: 0, 50: 678, 51: 123, 52: 204, 53: 225, 54: 100, 55: 13, 56: 88, 57: 302, 58: 81, 59: 111, 60: 141, 61: 98, 62: 57, 63: 73, 64: 334, 65: 422, 66: 49, 67: 0, 68: 600, 69: 86, 70: 55, 71: 162, 72: 138, 73: 50, 74: 296, 75: 30, 76: 153, 77: 186, 78: 68, 79: 39, 80: 173, 81: 0, 82: 276, 83: 192, 84: 66, 85: 116, 86: 89, 87: 385, 88: 209, 89: 121, 90: 68, 91: 22, 92: 52, 93: 262, 94: 261, 95: 70, 96: 85, 97: 298, 98: 170, 99: 126, 100: 145, 101: 17, 102: 53, 103: 56, 104: 0, 105: 97, 106: 114, 107: 72, 108: 42, 109: 22, 110: 211, 111: 370, 112: 175, 113: 111, 114: 27, 115: 62, 116: 104, 117: 118, 118: 248, 119: 0, 120: 58, 121: 20, 122: 52, 123: 20, 124: 97, 125: 119, 126: 107, 127: 108, 128: 79, 129: 42, 130: 0, 131: 281, 132: 83, 133: 57, 134: 61, 135: 50, 136: 50, 137: 22, 138: 37}, '2022-01-01': {0: 938, 1: 1501, 2: 377, 3: 1455, 4: 17, 5: 0, 6: 815, 7: 562, 8: 534, 9: 628, 10: 178, 11: 332, 12: 0, 13: 177, 14: 311, 15: 614, 16: 50, 17: 121, 18: 343, 19: 314, 20: 356, 21: 587, 22: 498, 23: 67, 24: 222, 25: 230, 26: 210, 27: 0, 28: 237, 29: 131, 30: 222, 31: 74, 32: 12, 33: 0, 34: 79, 35: 53, 36: 397, 37: 351, 38: 253, 39: 269, 40: 63, 41: 211, 42: 53, 43: 163, 44: 209, 45: 287, 46: 364, 47: 59, 48: 49, 49: 0, 50: 290, 51: 55, 52: 113, 53: 76, 54: 85, 55: 83, 56: 190, 57: 166, 58: 72, 59: 108, 60: 119, 61: 121, 62: 25, 63: 46, 64: 163, 65: 204, 66: 76, 67: 0, 68: 250, 69: 76, 70: 148, 71: 161, 72: 97, 73: 44, 74: 150, 75: 34, 76: 144, 77: 189, 78: 73, 79: 27, 80: 109, 81: 0, 82: 90, 83: 185, 84: 48, 85: 110, 86: 198, 87: 216, 88: 139, 89: 59, 90: 34, 91: 45, 92: 116, 93: 187, 94: 164, 95: 34, 96: 80, 97: 45, 98: 78, 99: 82, 100: 54, 101: 14, 102: 28, 103: 31, 104: 48, 105: 52, 106: 97, 107: 29, 108: 56, 109: 33, 110: 84, 111: 212, 112: 111, 113: 128, 114: 18, 115: 81, 116: 32, 117: 115, 118: 192, 119: 0, 120: 36, 121: 194, 122: 17, 123: 55, 124: 98, 125: 104, 126: 83, 127: 101, 128: 54, 129: 36, 130: 0, 131: 156, 132: 33, 133: 104, 134: 101, 135: 31, 136: 46, 137: 66, 138: 20}, '2022-02-01': {0: 612, 1: 912, 2: 325, 3: 892, 4: 11, 5: 0, 6: 706, 7: 310, 8: 439, 9: 563, 10: 134, 11: 140, 12: 0, 13: 153, 14: 281, 15: 399, 16: 49, 17: 90, 18: 204, 19: 231, 20: 100, 21: 318, 22: 255, 23: 63, 24: 309, 25: 181, 26: 205, 27: 0, 28: 121, 29: 84, 30: 117, 31: 80, 32: 143, 33: 0, 34: 65, 35: 64, 36: 227, 37: 271, 38: 133, 39: 290, 40: 47, 41: 156, 42: 0, 43: 176, 44: 153, 45: 244, 46: 300, 47: 14, 48: 30, 49: 0, 50: 126, 51: 46, 52: 81, 53: 69, 54: 165, 55: 48, 56: 79, 57: 91, 58: 31, 59: 95, 60: 138, 61: 87, 62: 34, 63: 39, 64: 101, 65: 111, 66: 19, 67: 0, 68: 15, 69: 26, 70: 0, 71: 88, 72: 81, 73: 53, 74: 135, 75: 62, 76: 92, 77: 141, 78: 57, 79: 32, 80: 71, 81: 34, 82: 357, 83: 92, 84: 50, 85: 82, 86: 97, 87: 128, 88: 75, 89: 54, 90: 23, 91: 28, 92: 57, 93: 108, 94: 138, 95: 48, 96: 79, 97: 109, 98: 52, 99: 54, 100: 73, 101: 27, 102: 20, 103: 26, 104: 86, 105: 48, 106: 54, 107: 27, 108: 39, 109: 61, 110: 67, 111: 110, 112: 127, 113: 147, 114: 0, 115: 60, 116: 23, 117: 68, 118: 101, 119: 23, 120: 25, 121: 93, 122: 35, 123: 25, 124: 52, 125: 72, 126: 50, 127: 84, 128: 78, 129: 43, 130: 0, 131: 82, 132: 34, 133: 84, 134: 13, 135: 13, 136: 37, 137: 69, 138: 13}, '2022-03-01': {0: 573, 1: 775, 2: 267, 3: 870, 4: 19, 5: 0, 6: 494, 7: 254, 8: 402, 9: 657, 10: 180, 11: 144, 12: 0, 13: 266, 14: 240, 15: 394, 16: 106, 17: 142, 18: 216, 19: 211, 20: 113, 21: 245, 22: 152, 23: 88, 24: 225, 25: 168, 26: 177, 27: 0, 28: 92, 29: 70, 30: 98, 31: 124, 32: 103, 33: 0, 34: 85, 35: 86, 36: 189, 37: 184, 38: 108, 39: 0, 40: 69, 41: 125, 42: 26, 43: 128, 44: 119, 45: 226, 46: 251, 47: 26, 48: 58, 49: 0, 50: 109, 51: 67, 52: 70, 53: 55, 54: 157, 55: 49, 56: 51, 57: 89, 58: 43, 59: 69, 60: 136, 61: 92, 62: 79, 63: 54, 64: 59, 65: 64, 66: 35, 67: 0, 68: 239, 69: 48, 70: 101, 71: 91, 72: 53, 73: 65, 74: 147, 75: 38, 76: 70, 77: 107, 78: 41, 79: 32, 80: 51, 81: 39, 82: 130, 83: 123, 84: 44, 85: 60, 86: 177, 87: 99, 88: 75, 89: 35, 90: 21, 91: 25, 92: 77, 93: 88, 94: 86, 95: 88, 96: 52, 97: 45, 98: 42, 99: 52, 100: 121, 101: 28, 102: 22, 103: 26, 104: 104, 105: 39, 106: 48, 107: 45, 108: 42, 109: 35, 110: 74, 111: 101, 112: 101, 113: 120, 114: 22, 115: 58, 116: 23, 117: 53, 118: 70, 119: 45, 120: 30, 121: 69, 122: 44, 123: 37, 124: 33, 125: 49, 126: 49, 127: 58, 128: 55, 129: 33, 130: 0, 131: 58, 132: 30, 133: 42, 134: 43, 135: 23, 136: 31, 137: 83, 138: 22}, '2022-04-01': {0: 356, 1: 595, 2: 231, 3: 444, 4: 0, 5: 0, 6: 220, 7: 145, 8: 185, 9: 394, 10: 140, 11: 112, 12: 0, 13: 104, 14: 139, 15: 236, 16: 102, 17: 121, 18: 77, 19: 174, 20: 108, 21: 133, 22: 105, 23: 53, 24: 195, 25: 114, 26: 155, 27: 11, 28: 88, 29: 40, 30: 102, 31: 91, 32: 142, 33: 0, 34: 66, 35: 36, 36: 90, 37: 114, 38: 64, 39: 262, 40: 46, 41: 87, 42: 47, 43: 87, 44: 64, 45: 93, 46: 114, 47: 15, 48: 95, 49: 0, 50: 85, 51: 40, 52: 30, 53: 51, 54: 81, 55: 38, 56: 66, 57: 52, 58: 43, 59: 59, 60: 121, 61: 53, 62: 44, 63: 22, 64: 59, 65: 64, 66: 47, 67: 0, 68: 194, 69: 26, 70: 59, 71: 37, 72: 47, 73: 51, 74: 146, 75: 36, 76: 43, 77: 120, 78: 37, 79: 16, 80: 52, 81: 22, 82: 151, 83: 51, 84: 35, 85: 52, 86: 71, 87: 32, 88: 39, 89: 20, 90: 25, 91: 25, 92: 48, 93: 44, 94: 35, 95: 40, 96: 30, 97: 41, 98: 24, 99: 45, 100: 44, 101: 17, 102: 15, 103: 19, 104: 39, 105: 32, 106: 45, 107: 35, 108: 21, 109: 16, 110: 34, 111: 44, 112: 46, 113: 29, 114: 20, 115: 51, 116: 17, 117: 45, 118: 52, 119: 31, 120: 29, 121: 34, 122: 21, 123: 16, 124: 26, 125: 39, 126: 22, 127: 45, 128: 48, 129: 20, 130: 0, 131: 35, 132: 18, 133: 39, 134: 22, 135: 30, 136: 71, 137: 15, 138: 11}, '2022-05-01': {0: 383, 1: 326, 2: 108, 3: 397, 4: 0, 5: 0, 6: 110, 7: 83, 8: 142, 9: 240, 10: 137, 11: 70, 12: 0, 13: 142, 14: 110, 15: 203, 16: 111, 17: 265, 18: 52, 19: 109, 20: 57, 21: 85, 22: 73, 23: 202, 24: 102, 25: 50, 26: 178, 27: 42, 28: 55, 29: 26, 30: 53, 31: 173, 32: 76, 33: 0, 34: 207, 35: 87, 36: 29, 37: 79, 38: 27, 39: 102, 40: 115, 41: 33, 42: 102, 43: 65, 44: 42, 45: 47, 46: 92, 47: 25, 48: 93, 49: 0, 50: 42, 51: 80, 52: 20, 53: 105, 54: 52, 55: 70, 56: 46, 57: 31, 58: 86, 59: 39, 60: 32, 61: 33, 62: 103, 63: 16, 64: 49, 65: 24, 66: 22, 67: 0, 68: 161, 69: 78, 70: 31, 71: 36, 72: 28, 73: 73, 74: 57, 75: 21, 76: 30, 77: 39, 78: 22, 79: 70, 80: 24, 81: 55, 82: 134, 83: 25, 84: 16, 85: 28, 86: 24, 87: 28, 88: 31, 89: 17, 90: 60, 91: 30, 92: 32, 93: 49, 94: 20, 95: 13, 96: 12, 97: 31, 98: 20, 99: 25, 100: 21, 101: 33, 102: 29, 103: 36, 104: 23, 105: 26, 106: 26, 107: 31, 108: 30, 109: 15, 110: 22, 111: 20, 112: 32, 113: 27, 114: 39, 115: 18, 116: 40, 117: 31, 118: 21, 119: 24, 120: 52, 121: 22, 122: 62, 123: 37, 124: 16, 125: 19, 126: 17, 127: 23, 128: 17, 129: 15, 130: 0, 131: 22, 132: 32, 133: 24, 134: 20, 135: 21, 136: 13, 137: 23, 138: 25}, '2022-06-01': {0: 613, 1: 1944, 2: 1826, 3: 494, 4: 0, 5: 244, 6: 928, 7: 798, 8: 219, 9: 1529, 10: 1029, 11: 526, 12: 122, 13: 195, 14: 173, 15: 1261, 16: 87, 17: 243, 18: 1179, 19: 217, 20: 464, 21: 952, 22: 353, 23: 148, 24: 166, 25: 187, 26: 134, 27: 124, 28: 321, 29: 221, 30: 193, 31: 224, 32: 75, 33: 0, 34: 277, 35: 77, 36: 253, 37: 174, 38: 343, 39: 283, 40: 73, 41: 295, 42: 108, 43: 138, 44: 102, 45: 1364, 46: 467, 47: 28, 48: 87, 49: 16, 50: 145, 51: 88, 52: 128, 53: 60, 54: 80, 55: 81, 56: 40, 57: 206, 58: 61, 59: 166, 60: 144, 61: 71, 62: 78, 63: 39, 64: 331, 65: 116, 66: 25, 67: 13, 68: 62, 69: 37, 70: 24, 71: 311, 72: 106, 73: 50, 74: 257, 75: 22, 76: 56, 77: 128, 78: 100, 79: 55, 80: 139, 81: 70, 82: 140, 83: 20, 84: 53, 85: 33, 86: 38, 87: 167, 88: 218, 89: 20, 90: 34, 91: 19, 92: 25, 93: 199, 94: 122, 95: 24, 96: 28, 97: 36, 98: 69, 99: 146, 100: 33, 101: 14, 102: 21, 103: 27, 104: 28, 105: 78, 106: 62, 107: 30, 108: 47, 109: 20, 110: 78, 111: 48, 112: 35, 113: 21, 114: 17, 115: 49, 116: 61, 117: 92, 118: 26, 119: 16, 120: 47, 121: 36, 122: 54, 123: 43, 124: 23, 125: 40, 126: 22, 127: 121, 128: 145, 129: 12, 130: 18, 131: 31, 132: 31, 133: 17, 134: 23, 135: 23, 136: 19, 137: 24, 138: 24}, '2022-07-01': {0: 349, 1: 283, 2: 163, 3: 318, 4: 67, 5: 328, 6: 121, 7: 96, 8: 205, 9: 219, 10: 89, 11: 60, 12: 153, 13: 68, 14: 135, 15: 181, 16: 53, 17: 94, 18: 65, 19: 96, 20: 67, 21: 57, 22: 67, 23: 59, 24: 134, 25: 94, 26: 78, 27: 142, 28: 33, 29: 29, 30: 45, 31: 64, 32: 65, 33: 76, 34: 81, 35: 55, 36: 44, 37: 83, 38: 15, 39: 46, 40: 84, 41: 45, 42: 56, 43: 54, 44: 50, 45: 48, 46: 90, 47: 17, 48: 56, 49: 27, 50: 66, 51: 37, 52: 34, 53: 63, 54: 58, 55: 27, 56: 45, 57: 74, 58: 51, 59: 61, 60: 80, 61: 45, 62: 65, 63: 34, 64: 27, 65: 30, 66: 18, 67: 35, 68: 47, 69: 31, 70: 24, 71: 40, 72: 18, 73: 30, 74: 44, 75: 26, 76: 31, 77: 32, 78: 29, 79: 29, 80: 45, 81: 14, 82: 54, 83: 31, 84: 37, 85: 24, 86: 32, 87: 20, 88: 40, 89: 32, 90: 22, 91: 17, 92: 30, 93: 29, 94: 20, 95: 52, 96: 34, 97: 25, 98: 26, 99: 28, 100: 72, 101: 17, 102: 15, 103: 22, 104: 28, 105: 24, 106: 28, 107: 19, 108: 25, 109: 25, 110: 38, 111: 19, 112: 27, 113: 26, 114: 15, 115: 22, 116: 28, 117: 24, 118: 33, 119: 13, 120: 57, 121: 40, 122: 22, 123: 14, 124: 18, 125: 23, 126: 20, 127: 38, 128: 20, 129: 14, 130: 36, 131: 24, 132: 18, 133: 39, 134: 14, 135: 40, 136: 16, 137: 21, 138: 13}, '2022-08-01': {0: 857, 1: 500, 2: 362, 3: 334, 4: 308, 5: 296, 6: 289, 7: 266, 8: 244, 9: 223, 10: 206, 11: 192, 12: 180, 13: 169, 14: 160, 15: 159, 16: 140, 17: 134, 18: 134, 19: 128, 20: 127, 21: 126, 22: 123, 23: 116, 24: 112, 25: 111, 26: 108, 27: 102, 28: 99, 29: 94, 30: 94, 31: 89, 32: 88, 33: 88, 34: 87, 35: 85, 36: 83, 37: 79, 38: 78, 39: 77, 40: 77, 41: 77, 42: 76, 43: 75, 44: 75, 45: 74, 46: 72, 47: 65, 48: 65, 49: 65, 50: 64, 51: 64, 52: 64, 53: 62, 54: 62, 55: 61, 56: 61, 57: 61, 58: 60, 59: 60, 60: 58, 61: 55, 62: 54, 63: 54, 64: 54, 65: 54, 66: 53, 67: 53, 68: 52, 69: 50, 70: 49, 71: 49, 72: 49, 73: 48, 74: 48, 75: 48, 76: 47, 77: 47, 78: 46, 79: 44, 80: 44, 81: 43, 82: 43, 83: 43, 84: 42, 85: 42, 86: 41, 87: 41, 88: 41, 89: 40, 90: 39, 91: 39, 92: 39, 93: 39, 94: 38, 95: 37, 96: 37, 97: 36, 98: 36, 99: 36, 100: 36, 101: 35, 102: 35, 103: 35, 104: 35, 105: 35, 106: 35, 107: 34, 108: 34, 109: 34, 110: 32, 111: 32, 112: 32, 113: 32, 114: 31, 115: 31, 116: 30, 117: 30, 118: 30, 119: 30, 120: 29, 121: 29, 122: 28, 123: 28, 124: 28, 125: 28, 126: 28, 127: 28, 128: 28, 129: 28, 130: 28, 131: 27, 132: 27, 133: 27, 134: 27, 135: 27, 136: 27, 137: 27, 138: 26}}
You have to instantiate the models since they are classes.
The code would be,
from statsforecast import StatsForecast
from statsforecast.models import CrostonClassic, CrostonSBA, CrostonOptimized, ADIDA, IMAPA, TSB
from statsforecast.models import SimpleExponentialSmoothing, SimpleExponentialSmoothingOptimized, SeasonalExponentialSmoothing, SeasonalExponentialSmoothingOptimized, Holt, HoltWinters
from statsforecast.models import HistoricAverage, Naive, RandomWalkWithDrift, SeasonalNaive, WindowAverage, SeasonalWindowAverage
from statsforecast.models import MSTL
from statsforecast.models import Theta, OptimizedTheta, DynamicTheta, DynamicOptimizedTheta
from statsforecast.models import AutoARIMA, AutoETS, AutoCES, AutoTheta
seasonality = 12 #Monthly data
models = [
ADIDA(),
CrostonClassic(),
CrostonSBA(),
CrostonOptimized(),
IMAPA(),
TSB(0.3,0.2),
Theta(season_length=seasonality),
OptimizedTheta(season_length=seasonality),
DynamicTheta(season_length=seasonality),
DynamicOptimizedTheta(season_length=seasonality),
AutoARIMA(season_length=seasonality),
AutoCES(season_length=seasonality),
AutoTheta(season_length=seasonality),
HistoricAverage(),
Naive(),
RandomWalkWithDrift(),
SeasonalNaive(season_length=seasonality),
SeasonalExponentialSmoothing(season_length=seasonality, alpha=0.2),
]
fcst = StatsForecast(df=df, models=models, freq='MS', n_jobs=-1,
fallback_model=SeasonalNaive(season_length=seasonality))
%time forecasts = fcst.forecast(9)
forecasts.reset_index()
forecasts = forecasts.round(0)
Here's a colab link fixing the error: https://colab.research.google.com/drive/1vwIImCoKzGvePbgFKidauV8sXimAvO48?usp=sharing
I want to collapse the rows of a dataframe such that each row represents an hour (the source data goes down to minute granularity). I don't want to lose the data, instead I want to create a dict so that the key of the dict is the minute (the source data doesn't go down to seconds) and then have the value be the value for that minute. See my brute force example at the bottom for what I'm really getting at.
I have data that looks something like this
import pandas as pd
from datetime import datetime, timedelta
import itertools
import random
from copy import deepcopy
fruits=['apple','banana','pear']
dts=[datetime(2022,1,1)+timedelta(minutes=x*random.randint(1,9)) for x in range(48)]
df=pd.DataFrame([x for x in itertools.product(fruits, dts)], columns=['fruit','dt'])
df['value1']=[random.randrange(0,100) for i in range(df.shape[0])]
df['value2']=[random.randrange(0,100) for i in range(df.shape[0])]
df['value3']=[random.randrange(0,100) for i in range(df.shape[0])]
# fruit dt value1 value2 value3
# 0 apple 2022-01-01 00:00:00 56 55 65
# 1 apple 2022-01-01 00:02:00 98 67 16
# 2 apple 2022-01-01 00:12:00 52 19 23
# 3 apple 2022-01-01 00:09:00 0 60 82
# 4 apple 2022-01-01 00:08:00 94 51 22
# ... ... ... ... ... ...
# 139 pear 2022-01-01 00:43:00 14 49 37
# 140 pear 2022-01-01 02:56:00 92 98 73
# 141 pear 2022-01-01 02:15:00 9 5 99
# 142 pear 2022-01-01 03:04:00 92 39 10
# 143 pear 2022-01-01 05:29:00 52 61 65
The best I can do using reasonable syntax is:
df['minute']=[x.minute for x in df.dt]
df['hour']=[x.hour+1 for x in df.dt]
df['date']=[x.replace(hour=0, minute=0) for x in df.dt]
def splitdict(x):
mydict={}
x=list(x)
for elem in x:
mydict[elem[0]]=elem[1]
return(mydict)
df.groupby(['fruit','date','hour']).apply(lambda x: splitdict(zip(df.minute,df.value1)))
Of course, this doesn't actually work (so I'll spare you the output). It returns the same dict for every row and it would only work for value1 (ie. I don't know how to get the results for value1, value2, value3 at once) I thought if I did a deepcopy that would resolve the repeating dict but it didn't so I took that out.
I did a horrendous for loop to brute force what I want. That looks like this:
#brute force
uniq=df.drop_duplicates(['fruit','date','hour'])
uniq=uniq[['fruit','date','hour']]
results=[]
for index, row in uniq.iterrows():
mydict=(df[(df['date']==row['date']) & (df['hour']==row['hour']) & (df['fruit']==row['fruit'])].loc[:,['minute','value1','value2','value3']]).to_dict('records')
value1dict={mydict[i]['minute']:mydict[i]['value1'] for i in range(len(mydict))}
value2dict={mydict[i]['minute']:mydict[i]['value2'] for i in range(len(mydict))}
value3dict={mydict[i]['minute']:mydict[i]['value3'] for i in range(len(mydict))}
results.append(pd.DataFrame({'date':row['date'], 'hour':row['hour'], 'fruit':row['fruit'] , 'value1':[value1dict], 'value2':[value2dict], 'value3':[value3dict]}))
results=pd.concat(results)
# date hour fruit value1 value2 value3
# 0 2022-01-01 1 apple {0: 56, 2: 98, 12: 52, 9: 0, 8: 94, 30: 90, 48: 92, 21: 16, 45: 9, 40: 58, 24: 31, 13: 45, 28: 86, 57: 47, 56: 66, 34: 69, 39: 12, 41: 92, 43: 12} {0: 55, 2: 67, 12: 19, 9: 60, 8: 51, 30: 95, 48: 62, 21: 97, 45: 1, 40: 89, 24: 52, 13: 5, 28: 67, 57: 16, 56: 13, 34: 54, 39: 86, 41: 45, 43: 50} {0: 65, 2: 16, 12: 23, 9: 82, 8: 22, 30: 76, 48: 98, 21: 23, 45: 14, 40: 87, 24: 45, 13: 87, 28: 29, 57: 75, 56: 26, 34: 25, 39: 70, 41: 97, 43: 89}
# 0 2022-01-01 2 apple {12: 31, 39: 42, 42: 87, 20: 86, 45: 62, 28: 86, 55: 59, 44: 83, 0: 26, 2: 97, 36: 55, 24: 44} {12: 74, 39: 55, 42: 16, 20: 12, 45: 92, 28: 2, 55: 98, 44: 44, 0: 63, 2: 54, 36: 76, 24: 55} {12: 97, 39: 81, 42: 76, 20: 71, 45: 26, 28: 56, 55: 61, 44: 93, 0: 90, 2: 87, 36: 28, 24: 52}
# 0 2022-01-01 3 apple {15: 72, 8: 84, 24: 97, 0: 54, 30: 10, 12: 50, 55: 51, 56: 31} {15: 30, 8: 42, 24: 96, 0: 76, 30: 58, 12: 44, 55: 82, 56: 57} {15: 41, 8: 11, 24: 40, 0: 89, 30: 22, 12: 51, 55: 57, 56: 60}
# 0 2022-01-01 5 apple {3: 14, 56: 79, 0: 25} {3: 3, 56: 61, 0: 49} {3: 2, 56: 29, 0: 32}
# 0 2022-01-01 4 apple {52: 53, 0: 50, 4: 53} {52: 32, 0: 64, 4: 26} {52: 88, 0: 28, 4: 59}
# 0 2022-01-01 6 apple {42: 40, 29: 52} {42: 30, 29: 82} {42: 50, 29: 33}
# 0 2022-01-01 1 banana {0: 6, 2: 10, 12: 43, 9: 77, 8: 19, 30: 59, 48: 67, 21: 17, 45: 92, 40: 69, 24: 25, 13: 86, 28: 81, 57: 72, 56: 35, 34: 22, 39: 61, 41: 8, 43: 56} {0: 63, 2: 92, 12: 49, 9: 22, 8: 2, 30: 92, 48: 96, 21: 21, 45: 62, 40: 23, 24: 77, 13: 41, 28: 64, 57: 49, 56: 30, 34: 59, 39: 63, 41: 54, 43: 85} {0: 27, 2: 83, 12: 35, 9: 37, 8: 70, 30: 94, 48: 16, 21: 19, 45: 71, 40: 5, 24: 26, 13: 91, 28: 16, 57: 42, 56: 8, 34: 31, 39: 93, 41: 57, 43: 65}
# 0 2022-01-01 2 banana {12: 0, 39: 53, 42: 13, 20: 91, 45: 88, 28: 66, 55: 46, 44: 24, 0: 41, 2: 32, 36: 69, 24: 11} {12: 83, 39: 21, 42: 41, 20: 81, 45: 79, 28: 37, 55: 28, 44: 2, 0: 51, 2: 87, 36: 63, 24: 82} {12: 6, 39: 57, 42: 18, 20: 3, 45: 74, 28: 58, 55: 46, 44: 25, 0: 45, 2: 96, 36: 37, 24: 14}
# 0 2022-01-01 3 banana {15: 10, 8: 14, 24: 60, 0: 48, 30: 82, 12: 70, 55: 39, 56: 4} {15: 81, 8: 55, 24: 8, 0: 64, 30: 72, 12: 45, 55: 79, 56: 45} {15: 95, 8: 90, 24: 36, 0: 47, 30: 88, 12: 12, 55: 86, 56: 75}
# 0 2022-01-01 5 banana {3: 47, 56: 39, 0: 60} {3: 60, 56: 15, 0: 72} {3: 48, 56: 86, 0: 16}
# 0 2022-01-01 4 banana {52: 49, 0: 30, 4: 86} {52: 39, 0: 85, 4: 5} {52: 64, 0: 22, 4: 96}
# 0 2022-01-01 6 banana {42: 2, 29: 26} {42: 15, 29: 54} {42: 61, 29: 58}
# 0 2022-01-01 1 pear {0: 39, 2: 55, 12: 25, 9: 98, 8: 14, 30: 82, 48: 59, 21: 77, 45: 8, 40: 75, 24: 19, 13: 92, 28: 39, 57: 63, 56: 95, 34: 77, 39: 77, 41: 41, 43: 14} {0: 54, 2: 11, 12: 63, 9: 12, 8: 38, 30: 34, 48: 96, 21: 27, 45: 19, 40: 87, 24: 83, 13: 28, 28: 22, 57: 25, 56: 38, 34: 66, 39: 80, 41: 80, 43: 49} {0: 98, 2: 71, 12: 97, 9: 54, 8: 70, 30: 22, 48: 31, 21: 4, 45: 47, 40: 42, 24: 28, 13: 68, 28: 65, 57: 73, 56: 32, 34: 1, 39: 73, 41: 39, 43: 37}
# 0 2022-01-01 2 pear {12: 93, 39: 57, 42: 12, 20: 98, 45: 69, 28: 60, 55: 77, 44: 16, 0: 96, 2: 16, 36: 76, 24: 15} {12: 23, 39: 63, 42: 59, 20: 15, 45: 66, 28: 50, 55: 18, 44: 87, 0: 33, 2: 15, 36: 9, 24: 90} {12: 80, 39: 50, 42: 98, 20: 12, 45: 54, 28: 90, 55: 67, 44: 37, 0: 86, 2: 2, 36: 51, 24: 64}
# 0 2022-01-01 3 pear {15: 9, 8: 71, 24: 3, 0: 94, 30: 53, 12: 90, 55: 28, 56: 92} {15: 5, 8: 85, 24: 77, 0: 53, 30: 26, 12: 62, 55: 2, 56: 98} {15: 99, 8: 4, 24: 14, 0: 86, 30: 50, 12: 17, 55: 70, 56: 73}
# 0 2022-01-01 5 pear {3: 57, 56: 2, 0: 65} {3: 32, 56: 16, 0: 90} {3: 88, 56: 74, 0: 80}
# 0 2022-01-01 4 pear {52: 23, 0: 32, 4: 92} {52: 4, 0: 93, 4: 39} {52: 30, 0: 97, 4: 10}
# 0 2022-01-01 6 pear {42: 43, 29: 52} {42: 63, 29: 61} {42: 80, 29: 65}
IIUC
cols_to_dict = ['value1', 'value2', 'value3']
out = df.assign(date=df['dt'].dt.date, hour=df['dt'].dt.hour, minute=df['dt'].dt.minute) \
.set_index('minute').groupby(['date', 'hour', 'fruit'])[cols_to_dict] \
.agg(dict).sort_index(level=['date', 'fruit', 'hour']).reset_index()
Output:
>>> out
date hour fruit value1 value2 value3
0 2022-01-01 0 apple {0: 67, 9: 82, 6: 33, 21: 74, 16: 99, 20: 82, ... {0: 46, 9: 47, 6: 57, 21: 21, 16: 8, 20: 96, 1... {0: 25, 9: 42, 6: 1, 21: 99, 16: 63, 20: 47, 1...
1 2022-01-01 1 apple {3: 30, 0: 36, 5: 81, 24: [51, 57, 59], 25: 77... {3: 40, 0: 32, 5: 14, 24: [18, 49, 58], 25: 60... {3: 83, 0: 80, 5: 17, 24: [37, 24, 34], 25: 16...
2 2022-01-01 2 apple {0: 49, 24: [64, 26], 40: 6, 18: 48, 4: 11, 16... {0: 77, 24: [53, 77], 40: 80, 18: 11, 4: 76, 1... {0: 0, 24: [62, 75], 40: 44, 18: 13, 4: 56, 16...
3 2022-01-01 3 apple {20: [88, 88], 28: 94, 54: 48, 25: 34} {20: [68, 34], 28: 55, 54: 15, 25: 46} {20: [16, 49], 28: 50, 54: 11, 25: 26}
4 2022-01-01 4 apple {36: 91} {36: 86} {36: 95}
5 2022-01-01 5 apple {33: 21, 36: 70, 8: 89} {33: 34, 36: 66, 8: 69} {33: 39, 36: 38, 8: 92}
6 2022-01-01 6 apple {45: 87} {45: 80} {45: 24}
7 2022-01-01 0 banana {0: 67, 9: 67, 6: 64, 21: 80, 16: 29, 20: 85, ... {0: 30, 9: 33, 6: 4, 21: 64, 16: 29, 20: 59, 1... {0: 92, 9: 37, 6: 93, 21: 40, 16: 49, 20: 97, ...
8 2022-01-01 1 banana {3: 12, 0: 73, 5: 6, 24: [19, 81, 44], 25: 11,... {3: 41, 0: 22, 5: 86, 24: [68, 41, 7], 25: 25,... {3: 94, 0: 52, 5: 15, 24: [19, 59, 12], 25: 2,...
9 2022-01-01 2 banana {0: 49, 24: [36, 87], 40: 35, 18: 26, 4: 21, 1... {0: 96, 24: [30, 45], 40: 86, 18: 33, 4: 2, 16... {0: 53, 24: [39, 22], 40: 84, 18: 7, 4: 47, 16...
10 2022-01-01 3 banana {20: [45, 27], 28: 58, 54: 96, 25: 90} {20: [14, 22], 28: 49, 54: 14, 25: 90} {20: [94, 50], 28: 19, 54: 26, 25: 65}
11 2022-01-01 4 banana {36: 64} {36: 62} {36: 43}
12 2022-01-01 5 banana {33: 83, 36: 4, 8: 34} {33: 13, 36: 36, 8: 37} {33: 2, 36: 63, 8: 94}
13 2022-01-01 6 banana {45: 22} {45: 2} {45: 0}
14 2022-01-01 0 pear {0: 55, 9: 29, 6: 30, 21: 57, 16: 37, 20: 63, ... {0: 74, 9: 38, 6: 18, 21: 47, 16: 47, 20: 34, ... {0: 25, 9: 75, 6: 36, 21: 60, 16: 94, 20: 68, ...
15 2022-01-01 1 pear {3: 20, 0: 22, 5: 1, 24: [94, 27, 44], 25: 73,... {3: 59, 0: 50, 5: 7, 24: [34, 15, 28], 25: 24,... {3: 90, 0: 71, 5: 75, 24: [4, 4, 63], 25: 73, ...
16 2022-01-01 2 pear {0: 42, 24: [83, 98], 40: 83, 18: 34, 4: 58, 1... {0: 55, 24: [13, 50], 40: 39, 18: 37, 4: 68, 1... {0: 69, 24: [80, 49], 40: 80, 18: 82, 4: 13, 1...
17 2022-01-01 3 pear {20: [33, 50], 28: 47, 54: 16, 25: 0} {20: [28, 26], 28: 74, 54: 66, 25: 13} {20: [40, 67], 28: 88, 54: 96, 25: 4}
18 2022-01-01 4 pear {36: 46} {36: 28} {36: 97}
19 2022-01-01 5 pear {33: 27, 36: 23, 8: 57} {33: 71, 36: 82, 8: 57} {33: 23, 36: 88, 8: 91}
20 2022-01-01 6 pear {45: 83} {45: 45} {45: 3}