Summary
I have a dataframe with date as index. When I tried to print it with st.write(data) I have te date which include the time ie 2003-03-25 00:00:00
I would like to have a normal data ie 2003-03-25 or even March 25 2003
I have a dataframe with date as index. When I tried to print it with st.write(data) I have te date which include the time ie 2003-03-25 00:00:00
I would like to have a normal data ie 2003-03-25 or even March 25 2003
Have a look on this example.
from datetime import datetime
import streamlit as st
import pandas as pd
# Build an example dataframe with datetime column.
# Init data.
data = {
'Date': [datetime.strptime('2023-02-17 10:45:00', '%Y-%m-%d %H:%M:%S'),
datetime.strptime('2023-02-18 10:30:00', '%Y-%m-%d %H:%M:%S')],
'Count': [4, 6]
}
# Convert data to pandas dataframe.
df = pd.DataFrame(data)
# Set Date as index.
df = df.set_index('Date')
st.write('### Init dataframe')
st.write(df)
# Set index as date.
df.index = df.index.date
st.write('### Index as date')
st.write(df)
Thx a lot. How to change the format to yyyy-mm-dd to dd-mm-yyyy ? And how to change the columns format (width of comlumns…)
Thanks a lot
Cordialement
Change date format.
from datetime import datetime
import streamlit as st
import pandas as pd
# Build an example dataframe with datetime column.
# Init data.
data = {
'Date': [datetime.strptime('2023-02-17 10:45:00', '%Y-%m-%d %H:%M:%S'),
datetime.strptime('2023-02-18 10:30:00', '%Y-%m-%d %H:%M:%S')],
'Count': [4, 6]
}
# Convert data to pandas dataframe.
df = pd.DataFrame(data)
# Set Date as index.
df = df.set_index('Date')
st.write('### Init dataframe')
st.write(df)
# Reset the index to get the Date column back.
df = df.reset_index()
st.write('### Restored Date column')
st.write(df)
# Modify the Date format
df['Date'] = df['Date'].dt.strftime('%d-%m-%Y')
# Set Date as index again.
df = df.set_index('Date')
st.write('### Index as date in dd-mm-yyyy format')
st.write(df)
Ok. Thx
An other question : with st.date_input() how to format the date in dd/mm/aaaa instead of yyyy/mm/dd ?
Thx a lot.
Cordialement
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
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.
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.
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.
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.