Forum Discussion
PROBLEMA SCRIPT PYTHON AND POWERBI
Good afternoon,
I'm trying to launch a forecast script inside PowerBi, specifically this one:
import pandas as pd
import statsmodels.api as sm
data = PerfilCliente[['FECHA', 'PESO']]
data['FECHA'] = pd.to_datetime(data['FECHA'])
data.set_index('FECHA', inplace=True)
data = data.sort_index()
data_diff = data.diff().dropna()
model = sm.tsa.ARIMA(data_diff, order=(1, 1, 1))
model_fit = model.fit()
forecast = model_fit.forecast(steps=90)
forecast_dates = pd.date_range(data.index[-1], periods=90, freq='D')
forecast_df = pd.DataFrame(forecast, index=forecast_dates, columns=['Predicción_Peso'])
forecast_df.reset_index(inplace=True)
forecast_df.rename(columns={'index': 'FECHA'}, inplace=True)
forecast_df['FECHA'] = forecast_df['FECHA'].dt.strftime('%Y-%m-%d')
result = forecast_df
The problem turns out that, when I run it, it gives the following error:
Detalles: "ADO.NET: ÞУŧћøñ ŝ¢ѓĭρť έřґσŕ.
<pi>NameError: name 'PerfilCliente' is not defined
</pi>"
I don't know if I'm not referencing the Client Profile table correctly, but when I reference others with a dataset directly written in Python, I don't get the error. The libraries and everything are correctly installed. Could you help me?
Thank you.
1 Reply
- AnonymousNot applicable
Hi, Syndicate_Admin
Make sure that PerfilCliente has been properly imported into the Python script. In Power BI, you need to use the pandas gallery to read datasets from Power BI's data model. You can do this by using the dataset variable provided by Power BI.
You can also refer to the code below:
import pandas as pd import statsmodels.api as sm # Import the dataset from Power BI PerfilCliente = dataset # Ensure the columns are correctly referenced data = PerfilCliente[['FECHA', 'PESO']] data['FECHA'] = pd.to_datetime(data['FECHA']) data.set_index('FECHA', inplace=True) data = data.sort_index() data_diff = data.diff().dropna() model = sm.tsa.ARIMA(data_diff, order=(1, 1, 1)) model_fit = model.fit() forecast = model_fit.forecast(steps=90) forecast_dates = pd.date_range(data.index[-1], periods=90, freq='D') forecast_df = pd.DataFrame(forecast, index=forecast_dates, columns=['Predicción_Peso']) forecast_df.reset_index(inplace=True) forecast_df.rename(columns={'index': 'FECHA'}, inplace=True) forecast_df['FECHA'] = forecast_df['FECHA'].dt.strftime('%Y-%m-%d') result = forecast_dfHow to Get Your Question Answered Quickly
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.