I am currently wanting to forecast a data using sarimax in tabpy, but i am getting the error of value count mismatch. How can i show the tableau forecasted values and the dates returned by the script dataframe.
code to get previous and forecasted values:
SCRIPT_REAL(
"
import pandas as pd
import numpy as np
from statsmodels.tsa.statespace.sarimax import SARIMAX
#create dataframe
df=pd.DataFrame(
{'Close Date':_arg1,
'Line Amount(MINR)':_arg2
}
)
# Convert dates to datetime format
df['Close Date'] = pd.to_datetime(df['Close Date'])
# Set the date as index
df.set_index('Close Date', inplace=True)
daily_sales_filtered = df['Line Amount(MINR)'].resample('D').sum()
# Fit SARIMA model to the filtered data
seasonal_order_ = (1, 1, 1, 12) # Example seasonal order, you may need to tune (P, D, Q, s)
model = SARIMAX(daily_sales_filtered, order=(5, 1, 0), seasonal_order=seasonal_order_)
model_fit = model.fit()
# Forecast future sales
forecast_steps = _arg3[0]
forecast = model_fit.forecast(steps=forecast_steps)
previous_forecast = df['Line Amount(MINR)'].tolist()
previous_forecast.extend(forecast.tolist())
return previous_forecast
"
,
ATTR([Close Date]),
ATTR([Line Amount(MINR)]),
[Months Forecast],
ATTR([EXTEND CLOSE DATE]))
code to get previous and forecasted dates:
DATE(SCRIPT_STR(
"
import pandas as pd
import numpy as np
from statsmodels.tsa.statespace.sarimax import SARIMAX
#create dataframe
df=pd.DataFrame(
{'Close Date':_arg1,
'Line Amount(MINR)':_arg2
}
)
# Convert dates to datetime format
df['Close Date'] = pd.to_datetime(df['Close Date'])
# Set the date as index
df.set_index('Close Date', inplace=True)
daily_sales_filtered = df['Line Amount(MINR)'].resample('D').sum()
# Fit SARIMA model to the filtered data
seasonal_order_ = (1, 1, 1, 12) # Example seasonal order, you may need to tune (P, D, Q, s)
model = SARIMAX(daily_sales_filtered, order=(5, 1, 0), seasonal_order=seasonal_order_)
model_fit = model.fit()
# Forecast future sales
forecast_steps = _arg3[0]
forecast = model_fit.forecast(steps=forecast_steps)
previous_forecast_date = [date.strftime('%Y-%m-%d') for date in df.index.to_pydatetime()]
previous_forecast_date.extend([date.strftime('%Y-%m-%d') for date in forecast.index.to_pydatetime()])
return previous_forecast_date
"
,
ATTR([Close Date]),
ATTR([Line Amount(MINR)]),
[Months Forecast]))
error recieveing while putiing date in columns and values in rows:
Unable to complete action
Error Code: 6605BCF0
Unexpected number of results returned by SCRIPT function. Function expected 104754 values; 104799 values were returned.
I have also deslected the aggregate mesaures from the analysis dropdown. The code is running fine in normal python environment but how to implement the same in the tableau ?
I have performed returning values as string also but how to convert those to numeric row values i am not getting it ?