Can dcc.Checklist handle nullable strings (NAType)? Getting a JSON error when using one

My Dash app uses a Pandas dataframe as its source data. One of the columns is a “string” dtype:

>>> df.Terms.unique()
<StringArray>
['12M', 'NG', <NA>, '6M', 'MO', '24M', 'STL']
Length: 7, dtype: string

The <NA> there is a NAType:

>>> type(df.Terms.unique()[2])
<class 'pandas._libs.missing.NAType'>

I have a dcc.Checklist that uses that column for its options and values:

rental_terms_checklist = html.Div([
    html.H5("Lease Length"),
    # Create a checklist for rental terms
    dcc.Checklist(
      id = 'terms_checklist',
      # Loop through the unique values in the 'Terms' column and create a dictionary for each value
      options = [{'label': term, 'value': term} for term in df['Terms'].unique()],
      # Set the default value to be all rental terms
      value = df['Terms'].unique(),
      # add some spacing in between the checkbox and the label
      # https://community.plotly.com/t/styling-radio-buttons-and-checklists-spacing-between-button-checkbox-and-label/15224/4
      inputStyle = {
        "margin-right": "5px",
        "margin-left": "5px"
      },
    ),
],
id = 'rental_terms_div'
)

This is giving me big fat TypeError: Type is not JSON serializable: NAType error when running my app. If I exclude the <NA>, the app functions normally.

What’s odd is that according to this PR, NAType should already be supported, so I’m not sure why I’m getting this JSON error. Any ideas?

1 Like