Search code examples
pythonpandasprophet

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices Prophet Error


I want to implement prophet algorithm and use my dataset on the model. After fit process i got following error in predict process. How can i solve this problem.?

import pandas as pd
import pystan
from prophet import Prophet
df_prophet = df[['date', 'rate']]
train_df = df_prophet[:-5]
train_df.columns = ['ds', 'y']
train_df['ds']= to_datetime(train_df['ds'])
model = Prophet()
model.fit(train_df)
test_df = df_prophet[-5:][['date']]
test_list = to_datetime(test_df.date).values
forecast = model.predict(test_list)

---> 11 forecast = model.predict(test_list)

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices


Solution

  • It's a nice attempt. You just need a few tweaks.

    1. By passing to_datetime(test_df.date).values you were creating a numpy array instead of a dataframe. Prophet expects a dataframe.
    2. The Prophet model and predictor expects columns labeled ds and y and you're changing the column names after you split the dataframe, so your test part isn't getting the columns renamed.
    3. You shouldn't need to import pystan since the Prophet module is built on it already.

    Try this:

    import pandas as pd
    from prophet import Prophet
    
    df_prophet = df[['Date', 'Volume']]
    df_prophet.columns = ['ds', 'y']
    train_df = df_prophet[:-5]
    train_df['ds']= pd.to_datetime(train_df['ds'])
    model = Prophet()
    model.fit(train_df)
    test_df = df_prophet[-5:][['ds']]
    forecast = model.predict(test_df)
    forecast