def convert_duels(df_events):
    """
    This function converts Wyscout duels that end with the ball out of field
    (subtype_id 50) into a pass for the player winning the duel to the location
    of where the ball went out of field. The remaining duels are removed as
    they are not on-the-ball actions.

    Args:
    df_events (pd.DataFrame): Wyscout event dataframe

    Returns:
    pd.DataFrame: Wyscout event dataframe in which the duels are either removed or transformed into a pass
    """

    # Shift events dataframe by one and two time steps
    df_events1 = df_events.shift(-1)
    df_events2 = df_events.shift(-2)

    # Define selector for same period id
    selector_same_period = df_events["period_id"] == df_events2["period_id"]

    # Define selector for duels that are followed by an 'out of field' event
    selector_duel_out_of_field = (
        (df_events["type_id"] == 1)
        & (df_events1["type_id"] == 1)
        & (df_events2["subtype_id"] == 50)
        & selector_same_period
    )

    # Define selectors for current time step
    selector0_duel_won = selector_duel_out_of_field & (
        df_events["team_id"] != df_events2["team_id"]
    )
    selector0_duel_won_air = selector0_duel_won & (df_events["subtype_id"] == 10)
    selector0_duel_won_not_air = selector0_duel_won & (df_events["subtype_id"] != 10)

    # Define selectors for next time step
    selector1_duel_won = selector_duel_out_of_field & (
        df_events1["team_id"] != df_events2["team_id"]
    )
    selector1_duel_won_air = selector1_duel_won & (df_events1["subtype_id"] == 10)
    selector1_duel_won_not_air = selector1_duel_won & (df_events1["subtype_id"] != 10)

    # Aggregate selectors
    selector_duel_won = selector0_duel_won | selector1_duel_won
    selector_duel_won_air = selector0_duel_won_air | selector1_duel_won_air
    selector_duel_won_not_air = selector0_duel_won_not_air | selector1_duel_won_not_air

    # Set types and subtypes
    df_events.loc[selector_duel_won, "type_id"] = 8
    df_events.loc[selector_duel_won_air, "subtype_id"] = 82
    df_events.loc[selector_duel_won_not_air, "subtype_id"] = 85

    # set end location equal to ball out of field location
    df_events.loc[selector_duel_won, "accurate"] = False
    df_events.loc[selector_duel_won, "not_accurate"] = True
    df_events.loc[selector_duel_won, "end_x"] = (
        100 - df_events2.loc[selector_duel_won, "start_x"]
    )
    df_events.loc[selector_duel_won, "end_y"] = (
        100 - df_events2.loc[selector_duel_won, "start_y"]
    )

    # df_events.loc[selector_duel_won, 'end_x'] = df_events2.loc[selector_duel_won, 'start_x']
    # df_events.loc[selector_duel_won, 'end_y'] = df_events2.loc[selector_duel_won, 'start_y']

    # Define selector for ground attacking duels with take on
    selector_attacking_duel = df_events["subtype_id"] == 11
    selector_take_on = (df_events["take_on_left"]) | (df_events["take_on_right"])
    selector_att_duel_take_on = selector_attacking_duel & selector_take_on

    # Set take ons type to 0
    df_events.loc[selector_att_duel_take_on, "type_id"] = 0

    # Set sliding tackles type to 0
    df_events.loc[df_events["sliding_tackle"], "type_id"] = 0

    # Remove the remaining duels
    df_events = df_events[df_events["type_id"] != 1]

    # Reset the index
    df_events = df_events.reset_index(drop=True)

    return df_events