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
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.