Pandas group in docker container - error!

Hi,
I have create a container with my python dash script. On my local machine it works fine. But in docker container I get this error:

ERROR:app:Exception on /_dash-update-component [POST]

Traceback (most recent call last):

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app

    response = self.full_dispatch_request()

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request

    rv = self.handle_user_exception(e)

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request

    rv = self.dispatch_request()

  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request

    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

  File "/usr/local/lib/python3.8/site-packages/dash/dash.py", line 1336, in dispatch

    response.set_data(func(*args, outputs_list=outputs_list))

  File "/usr/local/lib/python3.8/site-packages/dash/_callback.py", line 151, in add_context

    output_value = func(*func_args, **func_kwargs)  # %% callback invoked %%

  File "app.py", line 218, in gen_pie_chart

    df_group = df2_unique.groupby(['SAMPLE_TYPE'])

  File "/usr/local/lib/python3.8/site-packages/pandas/core/frame.py", line 7631, in groupby

    return DataFrameGroupBy(

  File "/usr/local/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 889, in __init__

    grouper, exclusions, obj = get_grouper(

  File "/usr/local/lib/python3.8/site-packages/pandas/core/groupby/grouper.py", line 862, in get_grouper

    raise KeyError(gpr)

KeyError: 'SAMPLE_TYPE'

For troubleshooting I have modified the script to a very simple script:

@app.callback(
    Output('pie_chart', 'figure'),
    Input("memory", "data")
)
def gen_pie_chart(data):
    #is_empty_1 = False
    
    data = read_data_on_database()
    df = pd.DataFrame(data)
    
    # drop duplicateson column ID_NUMERIC
    df_unique = df.drop_duplicates(subset=['ID_NUMERIC'], ignore_index=True)

  
    df2_unique = pd.DataFrame(df_unique)
        
    df_group = df2_unique.groupby(['SAMPLE_TYPE'])

    df_counts = df_group.size().reset_index(name='counts')
    
    

    fig_pran_lauf = px.pie(df_counts, values='counts', names='SAMPLE_TYPE',color='SAMPLE_TYPE', color_discrete_map= rgb_dic)
    fig_pran_lauf.update_traces(textinfo='value') #show the count of loc_id        
    fig_pran_lauf.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)',
                          'paper_bgcolor': 'rgba(0, 0, 0, 0)',
                          'font_color':'#C4CDD5',
                          'font_size': 24
                          })
    
    
    #del(df, df_group, df_unique)

    return fig_pran_lauf

The problem here is probably the group function? I hope you can support me…

Thanks
Torsten

This annoying error means that Pandas can not find your column name in your dataframe. Before doing anything with the data frame, use print(df.columns) to see dataframe 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.

Hi,
thanks for your post. My problem in this case was a broken connection to database. It works after fixing the problem.

BR
Torsten