Dash - error with timedelta and DatePickerRange

Hi,

I’m trying to create a script where a user selects a daterange, and dash will open each csv with that daterange (the folder has csv files 19-02-14.csv, 19-02-15.csv, etc) and display the data on a single graph. When I run the script I get this error:

TypeError: can only concatenate str (not “datetime.timedelta”) to str

Would love if someone could tell me what I’m doing wrong:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import csv
from datetime import datetime as dt
from datetime import timedelta
import re


app = dash.Dash(__name__)

colors = {
    'background': '#FFFFFF',
}

app.layout = html.Div([
    dcc.Tabs(id="tabs", children=[
        dcc.Tab(label='Date Range', children=[
            dcc.DatePickerRange(
                id='cal-tab3',
                min_date_allowed=dt(2019, 1, 15),
                max_date_allowed=dt(2019, 2, 17),
                initial_visible_month=dt(2019, 2, 1),
                start_date= dt(2019, 1, 15),
                end_date=dt(2019, 8, 25),
            ),
                dcc.Graph(
                    id='graph-tab3',
                )
        ]),
    ])
])


# callback for tab 2
@app.callback(
    dash.dependencies.Output('graph-tab3', 'figure'),
    [dash.dependencies.Input('cal-tab3', 'start_date'),
     dash.dependencies.Input('cal-tab3', 'end_date')]
    )
def update_tab3(start_date, end_date):

    if end_date is not None and start_date is not None:
        x = start_date
        y = end_date
        tdata = []
        while y >= x:
            thedate = dt.strptime(x, '%Y-%m-%d')
            date_string = str(thedate)
            date_string = re.sub(' 00:00:00', '', date_string)
            f = open((date_string + '.csv'))
            csvfile = csv.reader(f)
            csvlist = list(csvfile)
            tdata.extend(csvlist)
            x = start_date + timedelta(1)
        figure = {
            'data': [
                      {'x': tdata[0], 'y': tdata[13], 'type': 'line', 'name': 'Total L1', 'marker': {'color': 'rgb(0, 89, 234)'}},
                      {'x': tdata[0], 'y': tdata[17], 'type': 'bar', 'name': 'Diff L1', 'marker': {'color': 'rgb(0, 89, 234)'}},
                      ],
            'layout': {
                      'title': 'Ticket Stats',
                      'plot_bgcolor': colors['background'],
                  }
              }
return figure

if name == ‘main’:
app.run_server(debug=True)

x = start_date + timedelta(1)
here start_date is a string but timedelta is of type datetime.timedelta. you should convert start_date to datetime if you want to use timedelta directly. Also, I think you may have to specify days=1 or weeks=1 or some unit to the timedelta() function.

1 Like

And now I’ve learned that there’s more to data types than str/int/float/binary. Thanks for the help awsm :slight_smile: