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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Python Pandas date issue

I'm trying to use exponential smoothing in Pandas in Power BI and want the forecast always to commence after the maximum date in the actuals dataset, i.e. my last week of actuals data is 6/11/2022, so I want to forecast starting from there.

 

I'm trying to write some code to extract and feed this maximum date into the dataframe that will hold the finished predictions, but for some reason it's not correctly determining the maximum date. It always gives a value in 1970, which I assume to be the datetime equivalent of a zero value.

 

# 'dataset' holds the input data for this script
import pandas as pd
import numpy as np
dataset.set_index('Date')
dataset.index.freq='W'
from statsmodels.tsa.holtwinters import ExponentialSmoothing
model = ExponentialSmoothing(dataset['Volume'],trend='mul',seasonal='mul',seasonal_periods=52).fit()
lastweek = dataset.index.max()
range = pd.date_range(lastweek,periods=17,freq='W')
predictions = model.forecast(17)
predictions_range = pd.DataFrame({'Volume':predictions,'Date':range})

Capture112.PNG

Extra info: I've been able to get some different code to work in Jupyter Notebook but for some reason this doesn't work in Power BI:

# 'dataset' holds the input data for this script
import pandas as pd
import numpy as np
dataset.set_index('Date')
dataset.index.freq='W'
from statsmodels.tsa.holtwinters import ExponentialSmoothing
model = ExponentialSmoothing(dataset['Volume'],trend='mul',seasonal='mul',seasonal_periods=52).fit()
lastweek = dataset.index.max()
lastweekdate = lastweek.to_pydatetime().date()
range = pd.date_range(lastweekdate,periods=17,freq='W')
predictions = model.forecast(17)
predictions_range = pd.DataFrame({'Volume':predictions,'Date':range})

sdfs.PNG

 

Source is a very ordinary csv file which converts into dates and numbers:

Capture524.PNG

Would appreciate anyone's help!

1 REPLY 1
amitchandak
Super User
Super User

@Anonymous , These two lines have issues

 

lastweek = dataset.index.max()
lastweekdate = lastweek.to_pydatetime().date()

 

can you test this code outside on a sample data

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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