Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

Not able to run this code in python script on power query

import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from statsmodels.tsa.statespace.sarimax import SARIMAX
from scipy.stats import norm

dataset = dataset[['Start of Week','23756856']]
def datetimeix(dataset):
dataset['Start of Week'] = pd.DatetimeIndex(dataset['Start of Week'])
dataset.set_index('Start of Week', inplace = True)
return dataset
dataset = datetimeix(dataset)
data_train = dataset[:45]
data_for_dist_fitting = dataset[45:]
history = np.log(dataset.astype(float))
predictions= []
train_end =datetime(2022,2,28)
rolling_predictions = data_for_dist_fitting.copy()
i=0

for train_end in data_for_dist_fitting.index+timedelta(7):

train_data = history[:train_end+timedelta(7)]
model = SARIMAX(train_data,order=(1,1,1),seasonal_order=(1, 1, 1, 12),enforce_invertibility=False, enforce_stationarity=False)\

model_fit = model.fit()
output = model_fit.predict()
yhat = np.exp(output)
rolling_predictions[train_end] = yhat
predictions[i]= rolling_predictions.iloc[i, i+1]
i=i+1

 

predictions = pd.DataFrame({'Date': data_for_dist_fitting.index ,'#Prediction':predictions })

2 REPLIES 2
Anonymous
Not applicable

Hi 

I ran it on visual studio code and debugged it. It still is not working on power bi.

v-yalanwu-msft
Community Support
Community Support

Hi, @Anonymous ;

Sorry, since this is the Power query forum, your python will have some shortcomings, you can try the following code can be executed in pycharm or other software; Are there any error messages?
Secondly, you check whether you have a python environment installed below and related libraries, such as pandas.

Using Python in Power BI Power Query Editor - Power BI | Microsoft Learn


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.