Please help me with this KeyError: 'Date'

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

In what line do you get the error?
Can you show the full error message?

edited the post, I have included the full error

And where is line 18?

oh sorry, here is line 18

df[‘Date’] = pd.to_datetime(df[‘Date’])

Try to add a print(df.columns) before that line.
Perhaps there is no Date column in the dataframe.

this is what I got:

RangeIndex(start=0, stop=6, step=1)

I don’t understand why you get RangeIndex(start=0, stop=6, step=1) instead of a list of columns.

I’m not an expert with pandas, but looking at the error I would say that df doesn’t have a column called Date, and, since the dataframe is created by a search on a table, I would say that the table doesn’t have a column called Date.

I tried print(list(df.columns)) and this is what I got

[0, 1, 2, 3, 4, 5]

looks like columns names have been converted into numbers

Perhaps it’s getting confused because row objects are not exactly dictionaries?
Try with something like this:

pd.DataFrame(dict(row) for row in app_tables.price_table.search())

here’s the result:

image

There’s the Date column

1 Like

Thanks for this, it solve this problem, now, on to the next problem :pensive:

1 Like