如何在Dash中正确使用DatePickerRange?我不断收到TypeError

时间:2019-04-06 16:53:22

标签: python plotly-dash

我正在尝试在破折号中使用dcc.DatePickerRange,以允许用户输入开始日期和结束日期,以过滤数据框然后更新图形。但是,我不断收到以下错误

  

TypeError:类型对象无

下面是我的代码。不知道我要去哪里错了。任何帮助将不胜感激。

import dash
import dash_core_components as dcc 
import dash_html_components as html 
from dash.dependencies import Input, Output
import plotly.plotly as py
import plotly.graph_objs as go
import sqlite3
import pandas as pd
from functools import reduce
import datetime as dt

conn = sqlite3.connect('paychecks.db')

df_ct = pd.read_sql('SELECT * FROM CheckTotal',conn)
df_earn = pd.read_sql('SELECT * FROM Earnings', conn)
df_whold = pd.read_sql('SELECT * FROM Withholdings', conn)

data_frames = [df_ct, df_earn, df_whold]
df_paystub = reduce(lambda  left,right: pd.merge(left,right,on=['Date'], how='outer'), data_frames)

df_paystub['Date'] = pd.to_datetime(df_paystub['Date'], format='%Y-%m-%d')
dcc.DatePickerRange(
    id='date-picker-range',
    start_date_placeholder_text="Start Date",
    end_date_placeholder_text="End Date",
    calendar_orientation='vertical',
)

@app.callback(
    dash.dependencies.Output('pay', 'figure'),
    [dash.dependencies.Input('date-picker-range', 'start_date'),
    dash.dependencies.Input('date-picker-range', 'end_date')]
)

def figupdate(start_date, end_date):
    df = df_paystub
    df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
    df = df[(df['Date'] > start_date) & (df['Date'] < end_date)]
    figure={
        'data': [
            go.Bar(  
                x = df['Date'],
                y = df['CheckTotal'],
                name = 'Take Home Pay',
            ),
                go.Bar(
                x = df['Date'],
                y = df['EarnTotal'],
                name = 'Earnings',
            )
        ],
        'layout': go.Layout(
            title = 'Take Home Pay vs. Earnings',
            barmode = 'group',
            yaxis = dict(title = 'Pay (U.S. Dollars)'),
            xaxis = dict(title = 'Date Paid')
        )
    }
    return figure

1 个答案:

答案 0 :(得分:0)

结果是我需要向start_date添加一个end_datedcc.DatePickerRange属性。

dcc.DatePickerRange(
                id='date-picker-range',
                start_date_placeholder_text="Start Date",
                end_date_placeholder_text="End Date",
                start_date=df_paystub['Date'].iloc[0],
                end_date=df_paystub['Date'].iloc[-1],
            )