Charting Not Recognizing Datetime Objects within Dataframe

Iโ€™m having trouble creating a simple line chart from a data frame created with data from a SQL call. The df object looks like this:

Index, Date, Images_Added. Date is type - datetime64[ns].

You can see that Streamlit renders the data frame object as such:

However the chart that gets created with st.line_chart(df) is:

It seems that I should probably be using st.altair_chart and have tried to wrangle the data frame so that it will play nice with altair, but no luck so far. Any thoughts appreciated.

Hi @RickBL, welcome to the Streamlit community!

I suspect you might get the chart you are looking for if you make the Date column an index instead, but youโ€™re right, in the situation where st.line_chart doesnโ€™t give you what you want, using st.altair_chart is the way to proceed.

Best,
Randy

Thank you Randy. Changing index was the key. In case this will help others:

sql_query = pd.read_sql_query (โ€™โ€™โ€˜EXEC sp_SQLCALL @datain = โ€˜4009โ€™ โ€˜โ€™โ€™, cnxn)
df = pd.DataFrame(sql_query)
df[โ€˜Dateโ€™] = pd.to_datetime(df[โ€˜Dateโ€™])

new_df = df.set_index('Date', inplace=False)
print(new_df)

st.line_chart(new_df)
st.bar_chart(new_df)
1 Like

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