gpt4 book ai didi

python - 如何在 Dash 中正确使用 DatePickerRange?我不断收到类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:55 25 4
gpt4 key购买 nike

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

TypeError: type object None

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

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

最佳答案

结果我需要向 dcc.DatePickerRange 添加 start_dateend_date 属性。

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?我不断收到类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55551344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com