How to use Return range Values from date_input to filter dataframe

Hi team. Can somone please share an example of how to use the return values from date_input, when Range paramaters are used?

For example:

date = st.sidebar.date_input(

     "Select Confirmation Date Range",

    (datetime.date(2019, 7, 6),datetime.date(2020,7,6)))

st.write(date)

Returns:

(datetime.date(2019, 7, 6),datetime.date(2020,7,6)

assuming the default dates are what I select.

How do I isolate each date into a variable so I can use it to filter a dataframe?

thanx much!!

Hey @Joe_Pawlowski,

I used the min_value and max_value parameters from the documentation on date_input and i mocked up this example that returns one date from the date_input:

import streamlit as st
import datetime

st.title("Date range")

min_date = datetime.datetime(2020,1,1)
max_date = datetime.date(2022,1,1)

a_date = st.date_input("Pick a date", min_value=min_date, max_value=max_date)

##this uses streamlit 'magic'!!!! 
"The date selected:", a_date

This produces ‘a_date’ of a single value between the two dates specified (Jan 1, 2020 and Jan, 1, 2022):
date_range_ex · Streamlit

Happy Streamlit-ing!
Marisa

hey @Joe_Pawlowski,

So it’s getting late here and apparently I can’t read anymore! :woman_facepalming: :see_no_evil: :speak_no_evil: How embarrassing!!!

I fixed my example to show you how to isolate a single date from a returned tuple of dates when your trying to get a range of dates from date_input ( :dizzy_face:)

import streamlit as st
import datetime

st.title("Date range")

min_date = datetime.datetime(2020,1,1)
max_date = datetime.date(2022,1,1)

a_date = st.date_input("Pick a date", (min_date, max_date))

##this uses streamlit 'magic'!!!!
"The date selected:", a_date
"The type", type(a_date)
"Singling out a date for dataframe filtering", a_date[0]

here is a gif of how that works:
date_range_ex · Streamlit

Ahh. Smarty pants.

Thanks much.

Go home, get some rest.

:wink:

1 Like