How can I filter by month using streamlit date_input() input widget?

Question:

I have a column like this YYYY-MM-DD in a data frame (look image below).

enter image description here

Here is how .csv file looks like:

Data de la classe,Genere,ID,Durada,Preu
2022-09-07,H,RQWLPVCJ,1h 30min,21.00

(Some numbers/letters appear in orange. That is caused due to code sample)

I want to add a filter that shows only the rows depending on the month the user decided to filter by.

For e.g.: The user selected October as a filter. Then, he will only see rows with a MM equal to 10 (considering January is equal to 1).

Here is what the filter looks like and the new data frame created based on the filter, as I first tried:

month = st.sidebar.selectbox(
        "Selecciona el mes",
        ('Gener', 'Febrer', 'Març',
         'Abril', 'Maig', 'Juny',
         'Juliol', 'Agost', 'Setembre', 
         'Octubre', 'Novembre', 'Decembre')
)

dfSelection = df.query(
        "Data de la classe == @month"
)

#Show data frame with filters applied
st.table(dfSelection)

I have no idea how I can access the MM position and then filter it.

Any ideas on that?

Asked By: Adrià Martínez

||

Answers:

Here is one approach with code comments.

import streamlit as st
import pandas as pd

# months for selectbox options.
MONTHS = ('Gener', 'Febrer', 'Març',
          'Abril', 'Maig', 'Juny',
          'Juliol', 'Agost', 'Setembre',
          'Octubre', 'Novembre', 'Decembre')

# Create a sample dict for dataframe
data = {
    'Data de la classe': ['2022-09-27', '2022-09-28',
                          '2022-09-29', '2022-10-03',
                          '2022-10-04']
}

# Build df.
df = pd.DataFrame(data)
st.dataframe(df)

# Convert date string to datetime and put it in DateTime column.
df['DateTime'] = pd.to_datetime(df['Data de la classe'])

# Create MonthNumber column based from DateTime column.
df["MonthNumber"] = df["DateTime"].dt.month
st.dataframe(df)

# Add streamlit selectbox.
sel_month = st.sidebar.selectbox(label="Selecciona el mes", options=MONTHS)

# Get the month index based from selected month.
# We add 1 since index starts at 0 but our january starts at 1.
month_index = MONTHS.index(sel_month) + 1

# Execute query.
st.write('#### Query result')
dfSelection = df.query(
    "MonthNumber == @month_index"
)

# Show data frame with filters applied.
st.table(dfSelection)

# Show only the Data de la classe column.
dfSelection_classe = dfSelection[['Data de la classe']]
st.table(dfSelection_classe)

Output

enter image description here

Data from csv file.

Read csv file with pandas, converting it to dataframe with pd.read_csv()

import streamlit as st
import pandas as pd

# months for selectbox options.
MONTHS = ('Gener', 'Febrer', 'Març',
          'Abril', 'Maig', 'Juny',
          'Juliol', 'Agost', 'Setembre',
          'Octubre', 'Novembre', 'Decembre')

# Create a sample dict for dataframe
# data = {
    # 'Data de la classe': ['2022-09-27', '2022-09-28',
                        #   '2022-09-29', '2022-10-03',
                        #   '2022-10-04']
# }

# Build df.
# df = pd.DataFrame(data)

df = pd.read_csv('s.csv')
st.dataframe(df)

# Convert date string to datetime and put it in DateTime column.
df['DateTime'] = pd.to_datetime(df['Data de la classe'])

# Create MonthNumber column based from DateTime column.
df["MonthNumber"] = df["DateTime"].dt.month
st.dataframe(df)

# Add streamlit selectbox.
sel_month = st.sidebar.selectbox(label="Selecciona el mes", options=MONTHS)

# Get the month index based from selected month.
# We add 1 since index starts at 0 but our january starts at 1.
month_index = MONTHS.index(sel_month) + 1

# Execute query.
st.write('#### Query result')
dfSelection = df.query(
    "MonthNumber == @month_index"
)

# Show data frame with filters applied.
st.table(dfSelection)

# Show only the Data de la classe column.
dfSelection_classe = dfSelection[['Data de la classe']]
st.table(dfSelection_classe)

csv file s.csv

Data de la classe,others
2022-09-27,a
2022-09-28,ss
2022-09-28,25
2022-10-03,85
2022-10-04,548
Answered By: ferdy

I first converted my ‘Date’ column into a DateTime format with

df['Date'] = pd.to_datetime(df['Data'], yearfirst=True)

As I wanted to show the months there were on the data frame, this how the filter looked like:

selected_month = st.sidebar.multiselect(
    'Selecciona un mes', 
    df['Data'].dt.month.unique()
)
filtered_df = df.query(
    Data.dt.month == @selected_month
)
    

Now, to delete T00:00:00 from pd.to_datetime() I used the following line:

if len(filtered_df):
    filtered_df['Data'] = filtered_df['Data'].dt.strftime('%Y-%m-%d')
    st.table(filtered_dataframe)
Answered By: Adrià Martínez
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.