Only list-like objects are allowed to be passed to isn()

Question:

I have a dropdown menu:

                            dcc.Dropdown(
                                          id="select",
                                          options = list(all_df['Device'].unique()),
                                          value = list(all_df['Device'].unique()[0])
                                          )    
                            dcc.Graph(id = 'shared' 
                                          , figure={}
                                          ),

to enable a selection of a device (or devices) to plot later on:

@app.callback(
              Output("shared", "figure"), 
              Input("select", "value")
              )
def update_signal_chart(select):
    df4 = all_df[all_df['Device'].isin(select)] 

(…)

and whatever I try, it’s always resulting in

TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]

or

TypeError: only list-like objects are allowed to be passed to isin(), you passed a nonetype 

or the like. I don’t understand what I am doing wrongly resp. what I shall change into what. The data frame looks as:

all_df.head()
Out[63]: 
                        time        V+  ...     I_fil                 Device
0 2022-09-27 11:56:22.733740  7.980062  ...  5.035886                   A
1 2022-09-27 11:56:22.733940  7.982012  ...  5.032311                   A
2 2022-09-27 11:56:22.734140  7.983312  ...  5.027761                   A
3 2022-09-27 11:56:22.734340  7.983962  ...  5.022236                   A
4 2022-09-27 11:56:22.734540  7.982987  ...  5.016711                   A

Here is the entire code of the dash:

all_df = pd.read_csv("out.csv"
                                    #, parse_dates=['time']
                                    , dayfirst=True
                                    , skiprows=4
                                    , sep=","
                                    , decimal='.')
    
df2 = all_df.melt(id_vars=['Device','time'], value_vars=['V+','V-','I_A', 'I_fil'])

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, title='Dashboard', external_stylesheets=external_stylesheets)  

colors = {
        'background': '#000000',
        'text': '#f3ff00'
        }

# Define the app
app.layout = html.Div(
                    children = [
    
                            html.Div([
                                    html.H4('Dauertest'),
                                    html.Div(children = ""),

                                    # Draw graph
                                    dcc.Graph(id = 'General' 
                                              , figure={}
                                              ),
                                    ]),
                                    dcc.Checklist(
                                                  id="signals",
                                                  options = ['V+', 'V-', 'I_A', 'I_fil'],
                                                  value = ['V+', 'V-'],
                                                  inline=True
                                                  ),
                                    html.Br(),
                                    html.Br(),
                                    html.Br(),
                                    
                            # New row
                            html.Div([
                                html.Div([           
                                    dcc.Dropdown(
                                                  id="select",
                                                  options = list(all_df['Device'].unique()),
                                                  value = list(all_df['Device'].unique()[0])
                                                  ),                                    
                                    dcc.Graph(id = 'shared' 
                                              , figure={}
                                              ),
                                        #], className='six columns'),
                                        ], className='twelve columns'),
                                    ], className='row')
])


@app.callback(
              Output("General", "figure"), 
              Input("signals", "value")
              )
def update_line_chart(signals):
    df3=df2[df2['variable'].isin(signals)]    
    

    fig_general = px.scatter(df3
                              , x = "time"
                              , y = 'value'
                              , color = 'Device'
                              , symbol = 'variable'
                              , hover_name = "Device"
                              , template = 'plotly_dark'
                              #, marginal_y="rug"
                              ).update_layout(
                                              transition_duration = 500
                                              , autosize = True
                                              , height = 700
                                              )

    return fig_general

@app.callback(
              Output("shared", "figure"), 
              Input("select", "value")
              )
def update_signal_chart(select):
    # Subset data frame to choose between devices
    # df4 = all_df[all_df['Device'].isin(select)] 
    df4 = all_df[all_df['Device'] == select]

    fig_shared = make_subplots(rows = 4
                               , cols = 1
                               , shared_xaxes=True
                               , vertical_spacing=0.05
                               )
    
    fig_shared.add_trace(go.Scattergl(
                                    x = all_df['time']
                                    , y = all_df['V+']
                                    , mode = "markers"
                                    , name="V+"
                                    , connectgaps = False
                                    #, hoverinfo  = '"Device'
                                    , marker=dict(
                                    #               color = 'LightSkyBlue'
                                    #               , size = 20
                                    #               , line = dict(
                                    #                           color='MediumPurple'
                                    #                           , width=2
                                    #                           )
                                                    symbol = "circle"
                                                    )
                                    )
                          , row=4
                          , col=1
                          )
    
    fig_shared.add_trace(go.Scattergl(
                                    x = all_df['time']
                                    , y = all_df['V-']
                                    , name="V-"
                                    , mode = "markers"
                                    , marker=dict(
                                                  symbol = "star"                
                                                  )
                                    )
                        , row=3
                        , col=1
                        )
    
    fig_shared.add_trace(go.Scattergl(
                                    x = all_df['time']
                                    , y = all_df['I_A']
                                    , name="I_A"
                                    , mode = "markers"
                                    , marker=dict(
                                                  symbol = "diamond"                
                                                  )
                                    )
                        , row=2
                        , col=1
                        )
    
    fig_shared.add_trace(go.Scattergl(
                                    x = all_df['time']
                                    , y = all_df['I_fil']
                                    , name="I_fil"
                                    , mode = "markers"
                                    , marker=dict(
                                                  symbol = "x"                
                                                  )
                                    )
                        , row=1
                        , col=1
                        )
    
    fig_shared.update_layout(
                            height= 1000
                            , width = 1900
                            , title_text = ""
                            , template = 'plotly_dark'
                            #, font=dict(
                            #            family = "Courier New, monospace"
                            #            , size = 18
                            #            , color = "RebeccaPurple"
                            #            )
                            )

    fig_shared.show()
    
    fig_shared['layout']['yaxis']['title'] = 'I_fil / A'
    fig_shared['layout']['yaxis2']['title'] = 'I_A / A'
    fig_shared['layout']['yaxis3']['title'] = 'V- / V'
    fig_shared['layout']['yaxis4']['title'] = 'V+ / V'
    
    
    return fig_shared


# Run the app
if __name__ == "__main__":
    app.run_server(
                    debug=False
                    , port=8050
                    )

and the data: https://filebin.net/pag2dnsixti4ce9a

Asked By: Ben

||

Answers:

isin function is used to compare a series against a list, not a series against a value (str type in your problem). That is why you got this error only list-like objects are allowed to be passed to isin().

In your case, you want to retrieve all rows based on the selected device from a dropdown menu. You can achieve that like so:

df4 = all_df.query('Device==@select')
Answered By: Hamzah
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.