RAHUL DHANOLA
RAHUL DHANOLA

Reputation: 11

Unable to forecast the data in tableau using tapy

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 ?

Upvotes: 0

Views: 47

Answers (1)

Alex Blakemore
Alex Blakemore

Reputation: 11921

SCRIPT_XXX() are table calc functions. They send vectors of values to your Python function and expect a vector of values of the same size as a result.

The error message explains that your Python function returned a different size vector than was expected. You are sending back more values than you received as input.

You can control the contents, size and order of the vectors you send using the Edit Table Calc dialog. Your Python code determines the size and contents of the vector of values you return to Tableau.

If you aren’t familiar with table calcs, read about them in the Tableau documentation, especially the section on partitioning and addressing.

Upvotes: 0

Related Questions