Can't put date values on x axis of a line chart

Summary

Used gspread to import a google sheet into a dataframe, Trying to make a line chart with it but i cant seem to get the x axis to display the date values that i have in the sheet
Code snippet:
list = df.columns.tolist()

choice = st.multiselect(“Choose Data type”, list, default=“CO2”)

datas = df[choice]
st.line_chart(datas)
Expected behavior:

As far as my understanding goes, The x axis should display the date values in the sheet:
screen2

Actual behavior:

Debug info

  • Streamlit version: 1.13.0

Requirements file

gspread==5.4.0
pandas==1.5.0
streamlit==1.13.0

You should set the date column to be the index: df.set_index(['date], inplace=True).

1 Like

One issue is that when you do datas=df[choice], you’re actually dropping the date column from your dataset. You might want to do this instead

datas = df[[choice, "date"]] # Also include the "date" column
st.line_chart(datas, x="date", y=choice)
1 Like

This makes sense, i have tried the last line without making it include the date column and it didn’t work so this must be why, thanks!

so i tried it and it showed an error,
TypeError: unhashable type: ‘list’
it’s from:
# datas = df[[choice, "date"]] #
is the issue that i used x.tolist here?:

list = df.columns.tolist()
choice = st.multiselect("Choose Data type", list, default="Temperature")    

and if yes how can i fix this ? i tried adding list2 = tuple(list) and changing the following lines to use list2 instead of list but got the same error

Ah, I missed the fact that you were using a “multiselect”, which already returns a list. This should work

import pandas as pd
import streamlit as st

# Made-up data
df = pd.DataFrame(
    {
        "date": [
            "2021-01-01",
            "2021-01-02",
            "2021-01-03",
            "2021-01-04",
        ],
        "CO2": [400, 500, 600, 700],
        "humidity": [40, 80, 60, 20],
    }
)


cols = df.columns.tolist()
cols.remove("date")

choice = st.multiselect("Choose Data type", cols, default="CO2")

datas = df[choice + ["date"]]
st.line_chart(datas, x="date", y=choice)

worked like a charm, Thanks for your help :hearts:

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