✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

TypeError: '>=' not supported between instances of 'datetime.date' and 'str'

My code

import dash
import dash_table as dt
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from dash.dependencies import Input, Output
from datetime import date

df=pd.read_csv(‘Jan-21.csv’)
df_product=df.groupby([‘BILL_DATE’,‘DEPARTMENT’]).sum().reset_index()
df_product[‘NETAMT’]=round(df_product[‘NETAMT’],2)
df_product[‘BILL_DATE’]=pd.to_datetime(df_product[‘BILL_DATE’])
df_product[‘BILLDATE’]=df_product[‘BILL_DATE’].dt.date
df_product.set_index(‘BILLDATE’)
print(df_product[:5])

app = dash.Dash(name)

app.layout = html.Div([
dcc.DatePickerRange(
id=‘my-date-picker-range’,
reopen_calendar_on_clear=True,
clearable = True,
min_date_allowed=date(2021,1,1),
max_date_allowed=date(2021,1,31),
initial_visible_month=date(2021,1,1),
end_date=date(2021,1,31)
),
dt.DataTable(id=‘my-table’,
columns=[{‘name’:i,‘id’:i} for i in df_product],
data=df_product.to_dict(‘records’),
page_current=0,
page_size=15,
style_data_conditional=[
{
‘if’: {‘row_index’: ‘odd’},
‘backgroundColor’: ‘rgb(248, 248, 248)’
}
],
style_header={
‘backgroundColor’: ‘rgb(230, 230, 230)’,
‘fontWeight’: ‘bold’
}
)
])

@app.callback(
Output(‘my-table’, ‘data’),
[Input(‘my-date-picker-range’, ‘start_date’),
Input(‘my-date-picker-range’, ‘end_date’)]
)

def update_output(start_date, end_date):
dff = df_product[‘BILLDATE’].between(start_date,end_date)
print(start_date)
print(end_date)
data=dff.to_dict(‘records’)
print(data[:5])
return data

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

Hi @Dineshn

Tip for sharing code:
image

The error says that you are trying to compare two different types elements one as datetime and the other as string. But I can’t find this comparison in your code. :thinking:

Also tag your question as Dash (first option) instead of Plotly to receive more answer for Dash users.

2 Likes

@Eduardo
Thanks for Your response,

I am trying to use date as my filter, so once I select the date (From and To) it should display the data between the date period. Instead it is showing the complete date when I select the date also.

Hi @Eduardo,

I have modified my code a little bit, now I am not getting any error. Now the only thing is if I select the date in the date filter the data is not updated.

Like initially the data set time period for a month, so when I want to select a particular date period data is not changed in the data table.

#My Code

import dash
import dash_table as dt
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from dash.dependencies import Input, Output
from datetime import date

df=pd.read_csv(‘Jan-21.csv’)
df_product=df.groupby([‘BILL_DATE’,‘DEPARTMENT’]).sum().reset_index()
df_product[‘NETAMT’]=round(df_product[‘NETAMT’],2)
df_product[‘BILL_DATE’]=pd.to_datetime(df_product[‘BILL_DATE’])
df_product[‘BILL_DATE’]=df_product[‘BILL_DATE’].dt.date
print(df_product[:5])
df_product2=df_product.reset_index(drop=False)

app = dash.Dash(name)

app.layout = html.Div([
dcc.DatePickerRange(
id=‘my-date-picker-range’,
reopen_calendar_on_clear=True,
clearable = True,
min_date_allowed=df_product[‘BILL_DATE’].min(),
max_date_allowed=df_product[‘BILL_DATE’].max(),
initial_visible_month=df_product[‘BILL_DATE’].min(),
end_date=df_product[‘BILL_DATE’].max()
),
dt.DataTable(id=‘my-table’,
columns=[{‘name’:i,‘id’:i} for i in df_product],
data=df_product.to_dict(‘records’),
page_current=0,
page_size=15,
style_data_conditional=[
{
‘if’: {‘row_index’: ‘odd’},
‘backgroundColor’: ‘rgb(248, 248, 248)’
}
],
style_header={
‘backgroundColor’: ‘rgb(230, 230, 230)’,
‘fontWeight’: ‘bold’
}
)
])

@app.callback(
Output(‘my-table’, ‘data’),
[Input(‘my-date-picker-range’, ‘start_date’),
Input(‘my-date-picker-range’, ‘end_date’)]
)

def update_data(start_date, end_date):
df_product2=df_product.loc[start_date:end_date]
print(start_date)
print(end_date)
data=df_product2.to_dict(‘records’)
print(data[:5])
return data

@app.callback(
Output(‘my-table’, ‘data’),
[Input(‘my-date-picker-range’, ‘start_date’),
Input(‘my-date-picker-range’, ‘end_date’)]
)

def update_columns(start_date,end_date):
df_product2=df_product.loc[start_date:end_date]
columns=[{‘name’:i,‘id’:i} for i in df_product2.columns]
return columns

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

Hi @Dineshn

Quiz:
Why do you think that I spend time drawing so beautiful picture ? :thinking:

Solution:
I hate reading code in that way :rage:


import dash
import dash_table as dt
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from dash.dependencies import Input, Output
from datetime import date

df=pd.read_csv(‘Jan-21.csv’)
df_product=df.groupby([‘BILL_DATE’,‘DEPARTMENT’]).sum().reset_index()
df_product[‘NETAMT’]=round(df_product[‘NETAMT’],2)
df_product[‘BILL_DATE’]=pd.to_datetime(df_product[‘BILL_DATE’])
df_product[‘BILL_DATE’]=df_product[‘BILL_DATE’].dt.date
print(df_product[:5])
df_product2=df_product.reset_index(drop=False)

app = dash.Dash(name)

app.layout = html.Div([
dcc.DatePickerRange(
id=‘my-date-picker-range’,
reopen_calendar_on_clear=True,
clearable = True,
min_date_allowed=df_product[‘BILL_DATE’].min(),
max_date_allowed=df_product[‘BILL_DATE’].max(),
initial_visible_month=df_product[‘BILL_DATE’].min(),
end_date=df_product[‘BILL_DATE’].max()
),
dt.DataTable(id=‘my-table’,
columns=[{‘name’:i,‘id’:i} for i in df_product],
data=df_product.to_dict(‘records’),
page_current=0,
page_size=15,
style_data_conditional=[
{
‘if’: {‘row_index’: ‘odd’},
‘backgroundColor’: ‘rgb(248, 248, 248)’
}
],
style_header={
‘backgroundColor’: ‘rgb(230, 230, 230)’,
‘fontWeight’: ‘bold’
}
)
])

@app.callback(
Output(‘my-table’, ‘data’),
[Input(‘my-date-picker-range’, ‘start_date’),
Input(‘my-date-picker-range’, ‘end_date’)]
)

def update_data(start_date, end_date):
df_product2=df_product.loc[start_date:end_date]
print(start_date)
print(end_date)
data=df_product2.to_dict(‘records’)
print(data[:5])
return data

@app.callback(
Output(‘my-table’, ‘data’),
[Input(‘my-date-picker-range’, ‘start_date’),
Input(‘my-date-picker-range’, ‘end_date’)]
)

def update_columns(start_date,end_date):
df_product2=df_product.loc[start_date:end_date]
columns=[{‘name’:i,‘id’:i} for i in df_product2.columns]
return columns

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

Hey @Eduardo ,

Sorry, I was new to Plotly community and I didn’t knew to paste in proper format. and thanks your help.

Hi @Dineshn

You are improving :joy: :joy:

But still you are not shearing the code correctly, you must copy and paste your original code.

There are two big reason for that:

  1. Anyone can read the code much easy and detect some problems, for example in your attached code you are leaving a space between this:
@app.callback(
Output(‘my-table’, ‘data’),
[Input(‘my-date-picker-range’, ‘start_date’),
Input(‘my-date-picker-range’, ‘end_date’)]
)

and that:


def update_columns(start_date,end_date):
df_product2=df_product.loc[start_date:end_date]
columns=[{‘name’:i,‘id’:i} for i in df_product2.columns]
return columns

And if you are not copying your exact code, perhaps you will answer that in the code this space doesnt exist.

  1. Anyone can copy and paste your code and try to run it in their PC. if I copy your code as is showed, I will have a lot of error because it doesnt have any intended spaces.

If you want more answer you need to facilitate others users lecture.

thanks again, I appreciate your help.

import dash
import dash_table as dt
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from dash.dependencies import Input, Output
from datetime import date


df=pd.read_csv('Jan-21.csv')
df_product=df.groupby(['BILL_DATE','DEPARTMENT']).sum().reset_index()
df_product['NETAMT']=round(df_product['NETAMT'],2)
df_product['BILL_DATE']=pd.to_datetime(df_product['BILL_DATE'])
df_product['BILL_DATE']=df_product['BILL_DATE'].dt.date
print(df_product[:5])
df_product2=df_product.reset_index(drop=False)



app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.DatePickerRange(
        id='my-date-picker-range',
        reopen_calendar_on_clear=True,
        clearable = True,
        min_date_allowed=df_product['BILL_DATE'].min(),
        max_date_allowed=df_product['BILL_DATE'].max(),
        initial_visible_month=df_product['BILL_DATE'].min(),
        end_date=df_product['BILL_DATE'].max()
    ),
    dt.DataTable(id='my-table',
                columns=[{'name':i,'id':i} for i in df_product],
                data=df_product.to_dict('records'),
                page_current=0,
                page_size=15,
                style_data_conditional=[
                    {
                        'if': {'row_index': 'odd'},
                        'backgroundColor': 'rgb(248, 248, 248)'
                    }
                ],
                style_header={
                    'backgroundColor': 'rgb(230, 230, 230)',
                    'fontWeight': 'bold'    
                    }
                )
])


@app.callback(
    Output('my-table', 'data'),
    [Input('my-date-picker-range', 'start_date'),
     Input('my-date-picker-range', 'end_date')]
    )

def update_data(start_date, end_date):
    df_product2=df_product.loc[start_date:end_date]
    print(start_date)
    print(end_date)
    data=df_product2.to_dict('records')
    print(data[:5])
    return data   

@app.callback(
    Output('my-table', 'data'),
    [Input('my-date-picker-range', 'start_date'),
     Input('my-date-picker-range', 'end_date')]
    )

def update_columns(start_date,end_date):
    df_product2=df_product.loc[start_date:end_date]
    columns=[{'name':i,'id':i} for i in df_product2.columns]
    return columns  

if __name__ == '__main__':
    app.run_server(debug=False)

Hi @Dineshn

As I told you in the last post, you can’t leave line space between the @app.callback(Output…, Input…) and the def name function, it must be just in the following row.

image

Sorry Again. I have removed the other line spaces forgot to remove this one.

I see both callback with the same problem :grinning:

Are you stil having problems with the table update?

Yes :slightly_frowning_face:

:thinking:

When you print the data:

print(data[:5])

Is showing what you are expecting ??

That was just for the reference, to see the output.

and corrected the empty lines in between

import dash
import dash_table as dt
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from dash.dependencies import Input, Output
from datetime import date

df=pd.read_csv('Jan-21.csv')
df_product=df.groupby(['BILL_DATE','DEPARTMENT']).sum().reset_index()
df_product['NETAMT']=round(df_product['NETAMT'],2)
df_product['BILL_DATE']=pd.to_datetime(df_product['BILL_DATE'])
df_product['BILL_DATE']=df_product['BILL_DATE'].dt.date
print(df_product[:5])
df_product2=df_product.reset_index(drop=False)

app = dash.Dash(__name__)
app.layout = html.Div([
    dcc.DatePickerRange(id='my-date-picker-range',
                        reopen_calendar_on_clear=True,
                        clearable = True,
                        min_date_allowed=df_product['BILL_DATE'].min(),
                        max_date_allowed=df_product['BILL_DATE'].max(),
                        initial_visible_month=df_product['BILL_DATE'].min(),
                        end_date=df_product['BILL_DATE'].max()
                       ),
    dt.DataTable(id='my-table',
                columns=[{'name':i,'id':i} for i in df_product],
                data=df_product.to_dict('records'),
                page_current=0,
                page_size=15,
                style_data_conditional=[{'if': {'row_index': 'odd'},
                                         'backgroundColor': 'rgb(248, 248, 248)'
                                        }],
                 style_header={'backgroundColor': 'rgb(230, 230, 230)',
                               'fontWeight': 'bold'
                              })
])

@app.callback(
    Output('my-table', 'data'),
    [Input('my-date-picker-range', 'start_date'),
     Input('my-date-picker-range', 'end_date')]
)
def update_data(start_date, end_date):
    df_product2=df_product.loc[start_date:end_date]
    print(start_date)
    print(end_date)
    data=df_product2.to_dict('records')
    print(data[:5])
    return data   

@app.callback(
    Output('my-table', 'data'),
    [Input('my-date-picker-range', 'start_date'),
     Input('my-date-picker-range', 'end_date')]
    )
def update_columns(start_date,end_date):
    df_product2=df_product.loc[start_date:end_date]
    columns=[{'name':i,'id':i} for i in df_product2.columns]
    return columns  

if __name__ == '__main__':
    app.run_server(debug=False)

@Dineshn

You can’t use two different callback for the same Output.

Output('my-table', 'data'),

Usually Dash show a message pointing that error. :thinking:

Now corrected, Callback function

#First Call back
@app.callback(
    Output('my-table', 'data')

#Second
@app.callback(
    Output('my-table', 'columns'),

output in terminal

Hey @Dineshn

See that in your prompt command output is showing that the start date is None, is this affecting your code in any way?

Also you are passing as data:

data=df_product2.to_dict('records')

But you are printing only

print(data[:5])

That means could be something from data[5:] ? :thinking:

Also I think that if you change:

if __name__ == '__main__':
    app.run_server(debug=False)

For (debug=True) you can see more deatails of any error.

Good Luck. :grinning:

print(data[5])

is similar to print(data.head())

output initially:

Later, if select any date period the table data becomes blank (ie, data is not getting updated)

Then the problem is in the code:


df_product2=df_product.loc[start_date:end_date]

When it receives the start date and the end date is not getting any value, try entering the start day and the end day manually like [2021-01-01:2021-01-30] and see if works.

Also turn debug = True to see the error in Dash.