roudan
roudan

Reputation: 4210

when using dcc.Store(), TypeError: Object of type DataFrame is not JSON serializable

I am using dcc.Store() to share dataframe data between callback. and I got an error of TypeError: Object of type DataFrame is not JSON serializable. Thanks for your help.


@app.callback(
    [
    Output("well-table", "data"),
    ],
    [
    Input('load-area-data','n_clicks'),
    ],
    [
     State("select-area-dropdown", "value"),
     State('read-existing-data-radio','value'),
    ],
    
    prevent_initial_call=True,   # disable output in the first load
)
def change_area_data(n_clicks,area,read_existing_radio): 

...
    s=json.dumps(df_timedata)
    return (datatable_thisarea,
            s, 
           )
TypeError: Object of type DataFrame is not JSON serializable

Upvotes: 0

Views: 1824

Answers (1)

Daniel Al Mouiee
Daniel Al Mouiee

Reputation: 1200

This is usually due to collecting/passing/returning a raw dataframe rather than a JSON-compatible version of it which can be serialised. You haven't provided an in-depth error log for this issue, but I highly suspect that you are returning the df_timedata incorrectly back to a dash datatable.

In any case, make sure you convert the dataframe using something like pandas.DataFrame.to_dict() which can be serialised and then storing that in a dcc.Store. you can reconvert back to a dataframe using pandas.DataFrame.from_dict()

Upvotes: 2

Related Questions