简体   繁体   English

如何使用之间将字符串值转换为DateTimeIndex以进行熊猫范围选择?

[英]How to convert string value to DateTimeIndex for pandas range selection using between?

I have a pandas read excel method from where I get a dataframe and the parse_dates parameter is false by default. 我有一个从中读取数据的熊猫读取excel方法,默认情况下parse_dates参数为false。 For visualization with Dash, there are two drop downs for start time and end time and I need to filter the dataframe within this time range based on the value selected in the drop down, but somehow I am stuck here and it gives an exception 为了使用Dash进行可视化,开始时间和结束时间有两个下拉列表,我需要根据下拉列表中选择的值在此时间范围内过滤数据帧,但是不知何故,我被困在这里,这是一个例外

"TypeError: Index must be DatetimeIndex" “ TypeError:索引必须为DatetimeIndex”

df = pd.read_excel(datasource)

html.Div([
        html.Label("Please select a day", style={'textAlign': "center"}),
        dcc.Dropdown(
            id='daydropdown',
            options=[{'label': i, 'value': i} for i in dropdownDate],
            multi=False
        ),
        html.Label("Please select a start time", style={'textAlign': "center"}),
        dcc.Dropdown(
            id='starttimedropdown',
            options=[{'label': i, 'value': i} for i in dropdownStartTime],
            multi=False
        ),
        html.Label("Please select an end time", style={'textAlign': "center"}),
        dcc.Dropdown(
            id='endtimedropdown',
            options=[{'label': i, 'value': i} for i in dropdownEndTime],
            multi=False
        ),
])


@app.callback(
    Output('availability graph', 'figure'),
    [Input('daydropdown', 'value'),
     Input('starttimedropdown', 'value'),
     Input('endtimedropdown', 'value'),
     Input('vehicletype', 'value')]
)
def update_graph(selectedday, selectedstarttime, selectedendtime, selectedvehicletype):
    # get all the parking spaces for the selected day and for the selected vehicle type
    df1 = df[(df["Datum"] == selectedday) & (df["Klassifikation"] == selectedvehicletype)]
    # filter parking spaces by the time range selected
    start = pd.to_datetime(selectedstarttime)
    end = pd.to_datetime(selectedendtime)
    df2 = df1[df1['Uhrzeit'].between_time(start, end)]

I expect the df2 to be filtered according to the selected start and end time, but the exception is thrown in this line, df2 = df1[df1['Uhrzeit'].between_time(start, end)] 我希望df2根据所选的开始和结束时间进行过滤,但是此行中引发了异常, df2 = df1[df1['Uhrzeit'].between_time(start, end)]

The format of the selected time is HH:MM:SS 所选时间的格式为HH:MM:SS

Your format for Uhrzeit is not a datetime but just an object (it misses the date). Uhrzeit格式不是日期时间,而只是对象(它错过了日期)。 Even so formatting the time strings selectedstarttime and selectedendtime is only possible because pandas put the date of today in front of the time section: 即使这样格式化时间字符串selectedstarttimeselectedendtime也是可能的,因为熊猫将今天的日期放在时间部分的前面:

pd.to_datetime('08:22:22') = Timestamp('2019-08-06 08:22:22')

If you want to use between_time make sure that the object you are checking is a datetime object. 如果要使用between_time确保要检查的对象是日期时间对象。 The arguments in the function can be written as HH:MM, and dont have to be datetime: 函数中的参数可以写为HH:MM,而不必是日期时间:

df2['Urhzeit'].between_time('0:15', '0:45')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM