如何在Dash中正确使用DatePickerRange?我一直收到TypeError

问题描述 投票:1回答:1

我正在尝试使用短划线中的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
python plotly-dash
1个回答
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],
            )
© www.soinside.com 2019 - 2024. All rights reserved.