KeyError: <class 'pandas._libs.tslibs.timestamps.Timestamp'> when making datetime slider


I am trying to make a datetime slider but I am getting an error:

KeyError: <class ‘pandas._libs.tslibs.timestamps.Timestamp’>

KeyError: <class 'pandas._libs.tslibs.timestamps.Timestamp'>
File "/Users/indapa/miniconda3/lib/python3.9/site-packages/streamlit/scriptrunner/", line 557, in _run_script
    exec(code, module.__dict__)
File "pages/", line 21, in <module>
    date_range = st.sidebar.slider('Select Date Range',
File "/Users/indapa/miniconda3/lib/python3.9/site-packages/streamlit/elements/", line 152, in slider
    return self._slider(
File "/Users/indapa/miniconda3/lib/python3.9/site-packages/streamlit/elements/", line 227, in _slider
    data_type = SUPPORTED_TYPES[type(value[0])]

My code is:

solar_file = Path(__file__).parent.parent / "solar_production.csv"
data = pd.read_csv(solar_file, index_col=0)
data['Time'] = pd.to_datetime(data['Time'], errors='coerce')
data = data[pd.notna(data['Time'])]
start_date = data['Time'].min()
end_date = data['Time'].max()

# create a date slidebar to select date range
date_range = st.sidebar.slider('Select Date Range', start_date, end_date, (start_date, end_date))
st.write('You selected:', date_range)

I am not sure what the issue is. Perhaps I should not be calling to_datetime on my pandas dataframe?

The type of start_date and end_date is pandas.Timestamp, but st.slider only accepts datetime.datetime. You can use .to_pydatetime to convert the former to the latter.

1 Like