St.date_input() seems to not produce a date

Hi, i have a simple question. Why do I need to transform the output of st.date_input() to date format with pd.to_datetime() ?

I mean that if I do not do this:

col = st.columns(2)
start_date = pd.to_datetime(col[0].date_input("Date de début:", 
                                                  format="DD/MM/YYYY", 
                                                  value=histo.first_valid_index(), 
                                                  min_value=histo.first_valid_index(),
                                                  max_value=histo.last_valid_index()
                                                  )
                                )
end_date = pd.to_datetime(col[1].date_input("Date de fin:", 
                                                format="DD/MM/YYYY", 
                                                value=histo.last_valid_index(),
                                                max_value=histo.last_valid_index()
                                                )
                              )

histo = 100*histo.loc[start_date:end_date]/histo.loc[start_date]

this code does not work with the pd.to_datetime() transformation

Hi, @Jacques2101 I understand the problem you are facing with your code:
You are using st.date_input() to obtain date input from the user in the format specified by the format parameter. The issue you’re encountering is related to the data type of the date input.

Pandas Date Indexing: When you perform date-based indexing or calculations in pandas, it expects the index to be in datetime format. Pandas provides various date-related functionalities, and working with datetime objects allows you to perform operations like date subtraction, resampling, and more.

Consistent Datatypes: By converting the user input from st.date_input() to a datetime object using pd.to_datetime(), you ensure that you’re working with a consistent datetime datatype throughout your code. This consistency is essential for reliable date-based operations.

Try use this code:

import streamlit as st
import pandas as pd

# Assuming 'histo' is a pandas DataFrame

col = st.columns(2)
start_date = pd.to_datetime(col[0].date_input("Date de début:", 
                                              format="DD/MM/YYYY", 
                                              value=histo.first_valid_index(), 
                                              min_value=histo.first_valid_index(),
                                              max_value=histo.last_valid_index()
                                              )
                            )
end_date = pd.to_datetime(col[1].date_input("Date de fin:", 
                                            format="DD/MM/YYYY", 
                                            value=histo.last_valid_index(),
                                            max_value=histo.last_valid_index()
                                            )
                          )

# Now you can safely use start_date and end_date as datetime objects
histo = 100 * histo.loc[start_date:end_date] / histo.loc[start_date]

I hope this will help you to solve this problem. Thank you

it is the same code as mine no ?

st.date_input returns a date object, whereas pd.to_datetime returns a datetime object (aka Timestamp in pandas). My guess is that your pandas dataframe is indexed using datetimes and not dates.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.