0

I have a dataframe that includes a column "birthdate" but its of type object like this 1984-11-15 format y%-%M-%d. I want to convert it to date and just extract the year with no duplication in order to filter the dataframe based on the year.

I am using the multiselect option in order to allow the user to filter the dataframe choosing multiple options, but when i try to convert the column type the system crash and display the below error.

StreamlitAPIException : Every Multiselect default value must exist in options

Traceback:

File "f:\AIenv\streamlit\TEC_APPS\fit_in_out_leb.py", line 762, in <module>
    main()File "f:\AIenv\streamlit\TEC_APPS\fit_in_out_leb.py", line 366, in main
    db_stb =  st.sidebar.multiselect("Select  date of birth ",list(query_bd),default = query_bd)

what this error mean and how to fix it ?

code:


df['birthdate'] = pd.to_datetime(df['birthdate'])
query_bd = df.birthdate.unique()
db_stb =  st.sidebar.multiselect("Select  date of birth ",list(query_bd),default = query_bd)
DevLeb2022
  • 653
  • 11
  • 40

1 Answers1

1

Create a new column for year, this column is used to generate the unique options and select a frame based on year.

import streamlit as st
import pandas as pd

d = {
    'birthdate': ['2022-01-01', '2021-01-01', '2020-01-01', '2019-01-01', '2019-02-05']
}
df = pd.DataFrame(d)
df['birthdate'] = pd.to_datetime(df['birthdate'])
df['year'] = df['birthdate'].dt.year

query_bd = df.year.unique()
db_stb =  st.sidebar.multiselect("Select  date of birth ", query_bd, default=query_bd)

selected = df.loc[df['year'].isin(db_stb)]
st.write(selected['birthdate'].dt.date)

Output

enter image description here

ferdy
  • 4,396
  • 2
  • 4
  • 16
  • if i try your answer it crash and display : `"Every Multiselect default value must exist in options" streamlit.errors.StreamlitAPIException: Every Multiselect default value must exist in options` But if i convert the `query_bd ` into a list it works but the type becomes float and the result is **2020.0**,**2022.0** – DevLeb2022 Feb 26 '22 at 19:03
  • I use streamlit==1.3.1 and streamlit==1.6.0 on windows 10 and both works. According to the doc at https://docs.streamlit.io/library/api-reference/widgets/st.multiselect the options can take different types. – ferdy Feb 27 '22 at 00:42