Date at x-axis of line_chart

Hi Guys,

Is it feasible to display the date instead of nothing on the x-axis of the graph?
At the moment the index of my data frame is the date.

Thanks for your help.

Best regards
Chris

Hi @chris_klose :wave:,

Good question and thanks for reaching out. If I’m reading your question right, this is a bug on our end and shouldn’t be happening. Linked is an issue I just created that you can follow/comment on if you’d like.

Here’s a potential workaround: st.line_chart is looking for an index named ‘index’ so you have to rename and reset index. That’s not obvious and should just take index regardless of name!

An example code snippet that could help:

df = pd.DataFrame({

  'date': ['10/1/2019','10/2/2019', '10/3/2019', '10/4/2019'],
  'second column': [10, 20, 30, 40]
})

df

st.line_chart(df.rename(columns={'date':'index'}).set_index('index'))

If this doesn’t help or isn’t addressing your question directly, would you mind posting a code snippet so we can dive in deeper?

1 Like

Hey @tc1,

thanks for replying.
Right now I get data in this format:

So I used df.reset_index(drop=True) to get rid of the date.
The issue here is that if I leave it, I get the error you already described:

After dropping the date, I get:


and with it the display works with st.line_chart(df["Open"]).

What I’m actually looking for is a way to put the date on the x-axis.

Unfortunately it doesn’t work for me with the above described variant.
Is there another way to display the date on the x-axis, is it a bug and will it be fixed or am I just using it in the wrong way?

Thanks for your help.

Best regards
Chris

Hi @chris_klose

Could you provide a full code snippet with what you’re trying to do, including dataframe creation, so we’re operating from the same page?

If I try the snippet provided by @tc1, I’m able to get the dates to display on the x-axis

import streamlit as st
import pandas as pd

df = pd.DataFrame({
  'date': ['10/1/2019','10/2/2019', '10/3/2019', '10/4/2019'],
  'second column': [10, 20, 30, 40]
})

df = df.rename(columns={'date':'index'}).set_index('index')

df

st.line_chart(df)

So do I understand correctly, and the name of the index has to be… “index”?

Hi @drorata, that’s a good question!

You can use any name if you set it with ‘set_index’. If no name is set, Streamlit will use ‘index’ and if you don’t have an ‘index’ column, Streamlit will display an error.

Please, let me know if this helps you.
Thanks

Hey @drorata,

As @DaniCaminos mentioned, you no longer need to rename the column. This change shipped with Streamlit version 0.51.0

import streamlit as st
import pandas as pd

df = pd.DataFrame({
  'date': ['10/1/2019','10/2/2019', '10/3/2019', '10/4/2019'],
  'second column': [10, 20, 30, 40]
})

df = df.set_index('date')

df

st.line_chart(df)