我正在尝试在破折号中使用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

最佳答案

原来我需要向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],
            )

关于python - 如何在Dash中正确使用DatePickerRange?我不断收到TypeError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55551344/

10-11 21:05