Date column from Polars dataframe is getting displayed as datetime in streamlit

I am using Polars dataframe and it has date column which is getting displayed as datetime in st.write, st.dataframe, st.table

Below is a sample code just to recreate this issue:

import streamlit as st
import pandas as pd
import polars as pl

date_issue = pd.DataFrame({
    "Test": ["A","B","C","D","E"],
    "Date": ["1/1/2023","1/2/2023","1/3/2023","1/4/2023","1/5/2023"],
    "Result": [1,2,3,4,5]
    })

st.write(f"csv date type is: {date_issue['Date'].dtype}")

date_issue["Date"] = pd.to_datetime(date_issue["Date"],dayfirst=True)

st.write("After converting date type in pandas dataframe")
st.write(f"csv date type is: {date_issue['Date'].dtype}")
st.write(date_issue.dtypes)

date_issue_pl = pl.from_pandas(date_issue)

st.write("After converting pandas into polars dataframe")
st.write(date_issue_pl.dtypes)

date_issue_pl = date_issue_pl.with_columns(pl.col("Date").dt.date().alias('Date'))

st.write("After converting date type in polars dataframe")
st.write(date_issue_pl.dtypes)

st.write("This is dataframe")
st.dataframe(date_issue_pl)

st.write("This is st.write")
st.write(date_issue_pl)

st.write("This is st.table")
st.table(date_issue_pl)

App screenshot:

I have tried this same code in Jupyter notebook as well and there Polars Date column is getting displayed as Date column only.

So it seems like an issue when I run it in streamlit.

Version
Python 3.12.4

streamlit==1.38.0
pandas==2.2.2
polars==1.6.0

Originally faced this issue on streamlit hosted app streamlit hosted app

Appreciate any help here !!

I’ll have to defer to @lukasmasuch about what behavior is intended here, but at least for a solution, you can control date format with column configuration:

import streamlit as st
import pandas as pd
import polars as pl
from datetime import date

date_issue = pd.DataFrame({
    "Test": ["A","B","C","D","E"],
    "Date": ["1/1/2023","1/2/2023","1/3/2023","1/4/2023","1/5/2023"],
    "Result": [1,2,3,4,5]
    })

date_issue["Date"] = pd.to_datetime(date_issue["Date"],dayfirst=True)

date_issue_pl = pl.from_pandas(date_issue)

date_issue_pl = date_issue_pl.with_columns(pl.col("Date").dt.date().alias('Date'))

st.dataframe(
    date_issue_pl,
    column_config={
        "Date": st.column_config.DateColumn(
            "Date",
            format="DD.MM.YYYY",
        )
    }
)

Thanks alot :), this fixed the problem !!

1 Like

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

This looks like that there might be an issue with the to_pandas conversion logic in Polars. If you call to_pandas on this polars dataframe, the date values gets converted to datetime in the resulting Pandas Dataframe.