TypeError: ‘<’ not supported between instances of ‘str’ and 'Timestamp’

Hi all,

I have created a date_input widget in my streamlit app where the user can select a specific date. Based on this date, a dataframe will be sliced and presented to the user.
The problem is that when the user selects, for example, 2020/09/14 as startdate, streamlit will output the following error:

TypeError: ‘<’ not supported between instances of ‘str’ and 'Timestamp’

Thanks in advance

Hi @isksy -

Can you provide the code you are running that demonstrates the error? I suspect this is actually a pandas issue where pandas wants you to convert your string to a timestamp, but without seeing the code I’m just guessing.

Best,
Randy

Hi Randyzwitch,

Thank you for your response.
Here is my code.

# Here I create the date_input objects
start_date = st.sidebar.date_input('Start Date')
end_date = st.sidebar.date_input('Eind Date')

# The start_date and end_date will be input for the foillowing function
def SearchPortfolio(data, start_date, eind_date):

# Slice the data with the end date and start date and extract certain values for the output dataframe.
    df = data.loc[start_date:eind_date]
    portf_startvalue = df.loc[start_date,['Start Waarde']][0]
    portf_stortingen = df.loc[start_date:eind_date,['Stortingen']].sum()[0]
    portf_deponeringen = df.loc[start_date:eind_date,['Deponeringen']].sum()[0]
    portf_onttrekkingen = df.loc[start_date:eind_date,['Onttrekkingen']].sum()[0]
    portf_lichtingen = df.loc[start_date:eind_date,['Lichtingen']].sum()[0]
    portf_endvalue = df.loc[eind_date,['Eind Waarde']][0]

    overview = [portf_startwaarde, portf_stortingen, portf_deponeringen, portf_onttrekkingen, portf_lichtingen, 
               portf_eindwaarde]
    df_final = pd.DataFrame([overview], columns = ['Start Waarde', 'Stortingen', 'Deponeringen', 'Onttrekkingen', 'Lichtingen', 'Eind Waarde'])

    return df_final

Could this possibly be a typo problem? Your second date_input widget writes to end_date, but in the pandas portion of the code, you are referring to eind_date.

Hi randy,

I figured out what the problem is.
the Date_input widget returns a timestamp and this gives an error because in my data I do not have the timestamp, only date.
I fixed this by converting the date with strftime(%Y-%M-%D) method. Now it outputs the correct date form and the data can be sliced correctly. Thanks for your input.

1 Like