How to handle KeyError : 'date'?

How to handel KeyError : ‘date’?

actually, I want to visualize my prediction but an error orrcue. Is there anyone to resolve this.
Admin.py
import datetime

import pandas as pd
import plotly.graph_objs as go
import plotly.offline as opy
from django import forms
from django.contrib import admin, messages
from django.template.response import TemplateResponse
from django.urls import path
from django.utils.translation import gettext as _

from turboAI.build import build_id
from turboAI.forecast import forecast_id
from turboAI.models import JaAiParameters
from turboAI.utils import (fetch_project_data, fetch_visualize_data,
project_model_history, train_project_models)

class DateRangeForm(forms.Form):
start = forms.DateField(
widget=admin.widgets.AdminDateWidget())
end = forms.DateField(
widget=admin.widgets.AdminDateWidget())

def clean(self):
    cleaned_data = super().clean()
    errors = []

    if cleaned_data['end'] > datetime.date.today():
        errors.append(forms.ValidationError(
            _('End date cannot be in the future'), code='invalid_end'))

    if not cleaned_data['start'] < cleaned_data['end']:
        errors.append(forms.ValidationError(
            _('Start date must be smaller than End date'), code='invalid_start'))

    if errors:
        raise forms.ValidationError(errors)

    return cleaned_data

def model_graph(model_history):
# get data
val = model_history[‘val_loss’]
train = model_history[‘loss’]
x = [i for i in range(1, len(val) + 1)]

# init figure
fig = go.Figure()

# add plots
fig.add_trace(go.Scatter(x=x, y=val, name="Validation Loss"))
fig.add_trace(go.Scatter(x=x, y=train, name="Training Loss"))

# add meta
fig.update_layout(title="Model loss by Epoch",
                  xaxis_title="Epoch", yaxis_title="Loss Ratio",
                  xaxis=dict(tickmode="linear", tick0=1, dtick=1),
                  yaxis=dict(tickmode="linear", tick0=0, dtick=0.1, range=[0, 1]))

# return HTML
return opy.plot(fig, auto_open=False, output_type='div')

def pred_graph(actual, preds):
columns = list(sorted(actual.columns))

# init figure
fig = go.Figure()

# add plots
for column in columns:
    fig.add_trace(go.Scatter(x=actual.index, mode='lines+markers', hoverinfo='y + x',
                             y=actual[column], name=f'Actual', visible=False))
    fig.add_trace(go.Scatter(x=preds.index, mode='lines+markers', hoverinfo='y + x',
                             y=preds[column], name=f'Predicted', visible=False))

# add dropdown
buttons = []
for i, column in enumerate(columns):
    visibility = [j == i * 2 or j == 1 + i *
                  2 for j in range(len(columns) * 2)]
    button = dict(
        label=column,
        method='update',
        args=[{'visible': visibility}]
    )
    buttons.append(button)

# add meta
fig.update_layout(title='Predicted to Actual Comparison',
                  updatemenus=[dict(active=-1, x=1.02, y=0, xanchor='left', direction='up', buttons=buttons)])

# make dates prettier
fig.update_xaxes(tickformat="%d %b, %Y")

# return HTML
return opy.plot(fig, auto_open=False, output_type='div')

@admin.register(JaAiParameters)
class ProjectModelAdmin(admin.ModelAdmin):
# list
list_display = (‘str’, ‘lr’, ‘decay’, ‘epoch’, ‘dropout’,
‘batch_size’, ‘lstm_shape’, ‘dense_shape’, ‘optimizer’, ‘has_model’, ‘last_prediction’)
actions = [‘build’, ‘forecast’]

# change form
change_form_template = 'admin/ai_parameters/change_form.html'
fieldsets = (
    (None, {'fields': ('project_management',)}),
    ('Model', {'fields': ('batch_size', 'epoch'),
               'classes': ('', )}),
    ('Optimizer', {'fields': ('optimizer', 'lr', 'decay'),
                   'classes': ('', )}),
    ('Layers', {'fields': ('lstm_shape', 'dense_shape', 'dropout'),
                'classes': ('collapse', )}),
)

def build(self, request, queryset):
    count = 0

    for p in queryset:
        if build_id(p.project_management.id):
            count += 1
        else:
            messages.warning(request, f"Could not build model for {p}")

    messages.success(
        request, f"Successfully built models for {count} projects")

build.short_description = "Build models for selected Projects"

def forecast(self, request, queryset):
    count = 0

    for p in queryset:
        if forecast_id(p.project_management.id):
            count += 1
        else:
            messages.warning(
                request, f"Could not produce forecasts for {p}")

    messages.success(
        request, f"Successfully produced forecasts for {count} projects")

forecast.short_description = "Add forecasts for selected Projects"

def get_urls(self):
    return [
        path('<int:id>/visualize/predictions/', self.visualize_pred_view),
        path('<int:id>/visualize/model/', self.visualize_model_view)
    ] + super().get_urls()

def visualize_pred_view(self, request, id):
    context = dict(self.admin_site.each_context(request))

    data = {'start': datetime.date.today() - datetime.timedelta(days=30),
            'end': datetime.date.today()}
    form = DateRangeForm(data)

    if request.method == 'POST':
        form = DateRangeForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data

    opts = self.model._meta
    has_view_permission = request.user.has_perm(
        f'{opts.app_label}.view_{opts.model_name}')

    context.update({
        'title': f'Visualize {JaAiParameters._meta.verbose_name} Predictions',
        'opts': opts,
        'app_label': opts.app_label,
        'has_view_permission': has_view_permission,
        'original': JaAiParameters.objects.get(pk=id),
        'media': form.media + self.media,
        'form': form,
        'graph': None
    })

    vis_data = fetch_visualize_data(context['original'], data)
    actual_df = pd.DataFrame(vis_data['actual'].values())
    preds_df = pd.DataFrame(vis_data['predictions'].values())

    actual_df.index = actual_df['date']
    preds_df.index = preds_df['date']

    actual_df.drop(['id', 'project_management_id', 'date'],
                   axis=1, inplace=True)
    preds_df.drop(['id', 'project_management_id', 'date'],
                  axis=1, inplace=True)

    context.update({
        'graph': pred_graph(actual_df, preds_df)
    })

    return TemplateResponse(request, "admin/ai_parameters/visualize_pred.html", context)

def visualize_model_view(self, request, id):
    context = dict(self.admin_site.each_context(request))

    opts = self.model._meta
    has_view_permission = request.user.has_perm(
        f'{opts.app_label}.view_{opts.model_name}')

    context.update({
        'title': f'Visualize {JaAiParameters._meta.verbose_name} Model',
        'opts': opts,
        'app_label': opts.app_label,
        'has_view_permission': has_view_permission,
        'original': JaAiParameters.objects.get(pk=id)
    })

    context.update({
        'graph': model_graph(project_model_history(context['original']))
    })

    return TemplateResponse(request, "admin/ai_parameters/visualize_model.html", context)

end admin.py

First, when you’re posting code here, please enclosed the blocks of code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is important with Python.

Second, we’re probably going to need to see the complete traceback from the console log (the terminal session in which you’re running runserver) and not just the exception page from your browser. There’s no way to tell from this where the exception is being thrown in your code.

From what little bit of the error message available here, I’ll guess that the error is being thrown in one of these lines:

(They’re the only places I can find in your code where you might be trying to access a dictionary using a key of ‘date’. I don’t see anywhere in your code where you’re setting a ‘date’ key on those dicts, but I don’t know what the libraries you’re using might be doing.)

If you’re expecting some external code to add elements to those dicts, you may need to either run this through the debugger so that you can see what data exists, or else add a bunch of print statements to show it to you.

First of all, thank you so much for replying to my POST.
I shared my file with you please check it out. please solve my error as I am suck from last week on this error
here is the admin.py file

(Attachment admin.py is missing)

Attaching files doesn’t work here. You can go back to your original post and edit it to place the backtick lines before and after the blocks of code.

Anyway, the error is caused because you’re trying to reference a dictionary key named ‘date’ when that key doesn’t exist.

One place in the code where this could occur, is this line:

(Note: I’m not saying the error is here, I’m saying this is one of the places I’ve seen where the error could be thrown. That’s where having the complete traceback included would help.)

I don’t see anywhere in the code where you set actual_df['date'].

I can’t point to a specific location of the source of the error, because this error is being caused by something that is missing, not because of something there that is wrong.

You need to figure out what actual_df[‘date’] is supposed to be set to, and make sure it gets set. (Or, you could use the dict .get() method to supply a default value if the value is missing, if it would be appropriate to do so.)

This KeyError ‘date’ occurs due to the unavailability of data in database. So that the graph of prediction is not shown

Pandas KeyError occurs when we try to access some column/row label in our DataFrame that doesn’t exist. Usually, this error occurs when you misspell a column/row name or include an unwanted space before or after the column/row name… Before doing anything with the data frame, use print(df.columns) to see column exist or not.

print(df.columns)

I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.