What I’m trying to do:
I am trying to do some predictor app which predicts the price based on the historical price from the table using an ARIMA model, but everytime I run it, I always encounter this error: “KeyError: ‘Date’”
I have a table named price_table and it has the Date column in there, now I am wondering what’s wrong with it. sorry, I am just new in anvil. I have tried doing it in google colab and I don’t have any problems.
Code Sample:
here is my server side code
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import pandas as pd
from statsmodels.tsa.arima.model import ARIMA
@anvil.server.callable
def search_and_predict(Model, Capacity, Renewed, Unlocked, num_months):
df = tables.app_tables.price_table.search(
Model=Model,
Capacity=Capacity,
Renewed=Renewed,
Unlocked=Unlocked
)
df = pd.DataFrame(df)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
filtered_df = df[(df['Model'] == Model) & (df['Capacity'] == Capacity) & (df['Renewed'] == Renewed) & (df['Unlocked'] == Unlocked)]
model = ARIMA(filtered_df['New_Price'], order=(1, 0, 0))
model_fit = model.fit()
last_date = filtered_df.index[-1]
future_dates = pd.date_range(start=last_date, periods=num_months + 1, freq='M')
predictions = model_fit.predict(start=len(filtered_df), end=len(filtered_df) + num_months, typ='levels')
predicted_data = pd.DataFrame({'Date': future_dates, 'Predicted Price': predictions})
return predicted_data
Full Error:
KeyError: 'Date'
at /home/anvil/.env/lib/python3.10/site-packages/pandas/core/indexes/range.py:349
called from /home/anvil/.env/lib/python3.10/site-packages/pandas/core/frame.py:3761
called from pricePredictSSC, line 18