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