0

我正在创建一个程序,它允许我们选择数据框、状态和日期范围来识别设施列表。我不知道为什么,但日期过滤部分似乎是随机的。显然我错过了一些东西,但我不确定是什么。

例如,如果您选择“df2021”和“az”,则应该有两个设施的工作日期为 10/31/2021 和 01/03/2021。但是,在我的日期选择框中选择一个日期范围,如果我选择 01/01/2020 作为开始过滤日期和 12/31/2022 作为结束日期,我得到零结果(即,没有得到 01/ 2021 年 3 月 3 日和 2021 年 10 月 31 日)。奇怪的是,如果我选择 2021 年 1 月 1 日和 2021 年 12 月 31 日作为过滤的日期范围,它可以工作,但我觉得这是一个巧合,并且正在发生其他事情。我很困惑。任何帮助表示赞赏。

import tkinter as tk
from tkinter import ttk
import pandas as pd
from tkcalendar import DateEntry

def make_df(event=None):
    global df
    val = n3.get()
    if val == 'df2022':
        df = df2022
    else:
        df = df2021
    # update combobox 'state'
    values = ['all'] + sorted(df['state'].unique())
    state['values'] = values
    state.set('all') # select 'all' initially


def on_click():
    val = n.get()
    df2 = df if val == 'all' else df[ df['state'] == val ]
    beg = startdate.get()
    end = enddate.get()
    df2 = df2[(df2['workdate'] >= beg) & (df2['workdate'] <= end)]
    print(beg, end)
    print(df2)

    
#DATAFRAMES

df2022 = pd.DataFrame({
   'id': ['0','1','2','3'],
   'state': ['oh', 'mi','mi','oh'],
   'provname': ['abc facility','123 facility','acme facility', 'rural hospital'],
   'workdate':['01/01/2022', '01/03/2022', '06/08/2022', '12/05/2022']
})

df2021 = pd.DataFrame({
   'id': ['4','5','6', '7'],
   'state': ['al','al','az', 'az'],
   'provname': ['jimmy state facility','mack 10 facility','heathcliff facility', 'new facility'],
   'workdate':['03/03/2021', '12/13/2021', '10/31/2021', '01/03/2021']
})


window = tk.Tk()
window.title('Combobox')
window.geometry('600x400')
window.focus_set()

ttk.Label(window, text = "Select Dataframe:",
          font = ("Times New Roman", 12)).grid(column = 0,
          row = 1, padx = 3, pady = 29)

# Create Combobox for selecting dataframe
n3 = tk.StringVar()
da = ttk.Combobox(window, width = 60, textvariable = n3)
da['values'] = ['df2022', 'df2021']
da.grid(column = 1, row = 1, padx = 3, pady = 20)
da.bind('<<ComboboxSelected>>', make_df)  # use virtual event instead of button


# Dates TextBox Creation
startdate = DateEntry(window, values="Text", state="readonly", date_pattern="mm/dd/yyyy")
startdate.grid(row=3, column=0, padx=20, pady=5)

enddate = DateEntry(window, values="Text", state="readonly", date_pattern="mm/dd/yyyy")
enddate.grid(row=3, column=1, padx=20, pady=5)

#STATE FILTER
ttk.Label(window, text = "Main Menu",
          foreground ="black",
          font = ("Times New Roman", 20)).grid(row = 0, column = 1)

#FILTER STATE - PART 1
ttk.Label(window, text = "Select the State:",
          font = ("Times New Roman", 12)).grid(column = 0,
          row = 2, padx = 3, pady = 29)

n = tk.StringVar()
state = ttk.Combobox(window, width = 60, textvariable = n)
state.grid(column = 1, row = 2, padx = 3, pady = 20)

ttk.Button(window, text = "OK", command=on_click).grid(column = 2,
          row = 2, padx = 3, pady = 10)

window.mainloop()
4

0 回答 0