0

I need my output in a dataframe after using a return statement. print () shows my output but return statement gives me : {}

Here is my current code with print working Entire Code with current output

Here is my input dataframe format: input_dataframe

This is my body of the code

prediction = {}
df2 = df.rename(columns={'date': 'ds','load': 'y', 'account': 'article'})
list_articles = df2.article.unique()

def get_prediction(df):
    prediction = {}
    f2 = df.rename(columns={'date': 'ds','load': 'y', 'account': 
    'article'})
    list_articles = df2.article.unique()  
 for article in list_articles:
     article_df = df2.loc[df2['article'] == article]
     my_model = Prophet(weekly_seasonality= True, 
     daily_seasonality=True,seasonality_prior_scale=1.0)
     my_model.fit(article_df)
     future_dates = my_model.make_future_dataframe(periods=6, freq='MS')
     forecast = my_model.predict(future_dates)
     prediction[article] = forecast
return prediction

I need the return statement on prediction to do what print is showing. Currently all that returns using "return prediction" = {} - an empty dictionary

  • 1
    Your for-loop seems to be outside the function going by the indentation. The whole body of a function should be at the same indentation level – rdas Oct 05 '19 at 17:17
  • 1
    *I've been up 15 hours*: **get some sleep**. It is way more productive to get some rest at this point so you can spot simple errors like the indentation level of your `for` loop and `return` statement being wrong. – Martijn Pieters Oct 05 '19 at 17:24
  • thanks, but I fix that problem, I am now returning {} but yet print works – grim_reaper_1988 Oct 05 '19 at 17:28
  • Sorry, Stack Overflow aims to collect questions that are useful to others in the future, with the answers to solve the problems presented. This also means that questions and answers are 'static', in that you can't use the same question to solve an evolving and related set of problems one after another; editing your question invalidates the answers given each time. I've rolled back your edit. – Martijn Pieters Oct 05 '19 at 17:28
  • @MartijnPieters I did edit the question - this is different then a indent error - I am returning something totally different - please assist – grim_reaper_1988 Oct 05 '19 at 17:31
  • I know you did edit it, and I told you that that was not okay. Your question started with an indentation error, and the `SyntaxError` exception that that caused. – Martijn Pieters Oct 05 '19 at 17:33
  • Did I already mention that with working on this for 15 hours straight with no sleep you really need to get some sleep now? You are making simple errors that are simply caused by lack of rest. Let this go for 7-8 hours and take care of your mind first. – Martijn Pieters Oct 05 '19 at 17:33
  • As for you new question where "print does work", we don't know what output you are seeing when you print(), nor do we have enough code and context to reproduce what is happening. So your new question is off-topic right now, for lack of an [mcve]. – Martijn Pieters Oct 05 '19 at 17:35
  • This is literally my whole set of code - short and clean. I added a picture to show the entirety. I have changed the topic of the thread now and also update with code and pictures to show the entirety. thanks please help :) – grim_reaper_1988 Oct 05 '19 at 17:44
  • In your screenshot it is clear that the `get_prediction()` function is entirely redundant and unused. Your `for` loop uses the `list_articles` variable created on line 6 of the previous cell, and `prediction` is a global variable. Just remove the `print()` and you are all set, because `prediction` doesn't need returning. – Martijn Pieters Oct 05 '19 at 17:49
  • Your right i needed sleep - all set thanks for the help working – grim_reaper_1988 Oct 05 '19 at 19:14

1 Answers1

1

You need to fix your indentation:

prediction = {}
df2 = df.rename(columns={'date': 'ds','load': 'y', 'account': 'article'})
list_articles = df2.article.unique()

def get_prediction(df):
    prediction = {}
    f2 = df.rename(columns={'date': 'ds','load': 'y', 'account': 
    'article'})
    list_articles = df2.article.unique()  
    for article in list_articles:
        article_df = df2.loc[df2['article'] == article]
        my_model = Prophet(weekly_seasonality= True, 
        daily_seasonality=True,seasonality_prior_scale=1.0)
        my_model.fit(article_df)
        future_dates = my_model.make_future_dataframe(periods=6, freq='MS')
        forecast = my_model.predict(future_dates)
        prediction[article] = forecast
    return prediction

The for loop needs to be under the function, the content of the for loop needs to be under the for loop, and the return needs to be under the function.

rassar
  • 5,412
  • 3
  • 25
  • 41