Getting ValueError: ('Lengths must match to compare', (36,), (1,))

Viewed 39

Month dropdown list unable to display value according to the year selected.

Error message:

ValueError: ('Lengths must match to compare', (36,), (1,))


I had a year dropdown list, require to display the month list based on the year selected and the date list according to the month selected.
Date: 01/02/2021, 29/02/2021, 20/12/2021, 05/06/2021, 25/07/2021, 01/05/2022, 20/03/2022, 21/09/2022, 30/07/2022


Year list : 2021, 2022

Year selected: 2021
Month list: Feb, June, July, Dec

Month selected: Feb
Date list: 01/02/2021, 29/02/2021

Code:

df['year'] = pd.to_datetime(df['Date'], format = '%d/%m/%Y', errors='coerce').dt.year


month_labels = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug',9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
df['month'] = pd.to_datetime(df['Date'], format = '%d/%m/%Y', errors='coerce').dt.month
df['monthname']= df['month'].apply(lambda x: month_labels[x]) 


df['date'] = pd.to_datetime(df['Date'], format = '%d/%m/%Y', errors='coerce')


df.sort_values(by= ['year', 'month', 'Date'], inplace = True)         
month_cat = list(df['monthname'].unique())            

sort_mm = sorted(month_cat, key=lambda m: datetime.strptime(m, "%b"))

sort_yr = df['year'].drop_duplicates()

 dbc.Row([
    dbc.Col([
        html.H3('Year'),
        html.Br(),
        
        dcc.Dropdown(id='year_dd', value= [df['year'].max()],
                      options = [{'label':x, 'value':x} 
                                for x in sort_yr],
          
                    ),
        html.Br(),
        
        html.H3('Month'),
        html.Br(),
        
        dcc.Dropdown(id='month_dd', value= ''                          
                      )


#create month selection based on year
@app.callback(
    Output('month_dd','options'),
    Input('year_dd', 'value')
    )


def update_dd (year_dd):


    year_month= df.drop_duplicates(['Date'], inplace= False)

    relevant_month= year_month[df['year'] == year_dd]['monthname'].values.tolist()

    month_opt= [dict(label=x,value=x)for x in relevant_month]


return month_opt


#set latest date as default value according to month dropdown
@app.callback(
    Output('month_dd','value'),
    Input('month_dd', 'options')
    )


def default_value(latest_date):
    monthvalue = latest_date[-1]['value']

    return monthvalue

####### can ignore this part, is just a reference ############
#create date dropdown according to month dropdown 
@app.callback(
    Output('date_dd','options'),
    Input('month_dd', 'value')
    )


def update_dd (month_dd):

    month_date= df.drop_duplicates(['Date'], inplace= False)

    relevant_date= month_date[df['monthname'] == month_dd]['Date'].values.tolist()

    date_option= [dict(label=x,value=x)for x in relevant_date]


    return date_option


#set latest date as default value according to month dropdown
@app.callback(
    Output('date_dd','value'),
    Input('date_dd', 'options')
    )


def default_value(latest_date):
    datevalue = latest_date[-1]['value']

return datevalue
0 Answers
Related