1 Reply Latest reply on Dec 2, 2019 9:19 AM by Nathan Mannheimer

    Tableau Facebook Prophet Python "date not in index'

    Anthony Philpott

      Hi Everyone

       

      I've successfully installed TabPy, have a connection to the server and am using Facebook Prophet (Facebook's time-series forecasting algorithm) to forecast future values. My script runs fine outside of Tableau but when I run inside Tableau I receive Keyerror "[date] not in index".

      I have tried to set date as an index two different ways first with df.set_index('date', inplace=True) and second with df.set_index('date', inplace=True, drop=False).

       

      Is there something else I am supposed to do to date prior to renaming it to ds in order to get this to work?

       

      My goal is to take the 30 day period of my projection and to output the sum of my y which is Pageviews.

       

      SCRIPT_REAL("

      import pandas as pd

      import numpy as np

      from fbprophet import Prophet

      from fbprophet.plot import plot_plotly

      import plotly.offline as py

      from fbprophet.plot import add_changepoints_to_plot

      from fbprophet.diagnostics import cross_validation

      from fbprophet.plot import plot_cross_validation_metric

      from fbprophet.diagnostics import performance_metrics

      py.init_notebook_mode()

      import matplotlib.pyplot as plt

      from fbprophet.plot import plot_forecast_component

      import plotly.graph_objs as go

      from fbprophet.plot import plot_yearly

      df=pd.DataFrame.from_csv(r'C:\Users\aphil0618\Desktop\RNN\models\script\simple_cnet_mobile.csv')

      df = df[['date','Pageviews']]

      df = df.rename(columns={'date': 'ds', 'Pageviews': 'y'})

      m = Prophet()

      m.fit(df)

      future = m.make_future_dataframe(periods=30)

      forecast = m.predict(future)

      ces = pd.DataFrame({

        'holiday': 'ces',

        'ds': pd.to_datetime(['2016-01-06', '2017-01-05', '2018-01-09',

                              '2019-01-08']),

        'lower_window': 0,

        'upper_window': 6,

      })

      apple = pd.DataFrame({

        'holiday': 'apple',

        'ds': pd.to_datetime(['2016-09-07', '2017-09-12', '2018-09-12','2019-10-10']),

        'lower_window': -7,

        'upper_window': 7,

      })

      samsung = pd.DataFrame({

        'holiday': 'samsung',

        'ds': pd.to_datetime(['2016-08-02', '2017-08-23', '2018-08-9','2019-08-25']),

        'lower_window': -7,

        'upper_window': 7,

      })

      mobile = pd.DataFrame({

        'holiday': 'mobile',

        'ds': pd.to_datetime(['2016-02-22', '2017-02-27', '2018-02-26','2019-02-25']),

        'lower_window': -7,

        'upper_window': 7,

      })

      prime = pd.DataFrame({

        'holiday': 'prime',

        'ds': pd.to_datetime(['2016-07-12', '2017-07-10', '2018-07-16','2019-07-15']),

        'lower_window': -4,

        'upper_window': 4,

      })

      black = pd.DataFrame({

        'holiday': 'black',

        'ds': pd.to_datetime(['2016-11-25', '2017-11-24', '2018-11-23']),

        'lower_window': -7,

        'upper_window': 2,

      })

      cyber = pd.DataFrame({

        'holiday': 'cyber',

        'ds': pd.to_datetime(['2016-11-28', '2017-11-27', '2018-11-26']),

        'lower_window': 0,

        'upper_window': 7,

      })     

      holidays = pd.concat((ces, apple, samsung, prime, black, cyber))

      m = Prophet(holidays=holidays)

      forecast = m.fit(df).predict(future)

      df_cv = cross_validation(m,horizon = '30 days')

      pred = df_cv['yhat']

      pred=np.transpose(np.array([_arg1]))

      return pred.tolist( )

      ",

      SUM([Pageviews]))