Date display with pandas

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

1 Like

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)

Output

image

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)

Output

1 Like

Have a look on dataframe and aggrid.

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.