sportsref/nfl/pbp.py
def _add_team_features(df):
    """Adds extra convenience features based on teams with and without
    possession, with the precondition that the there are 'team' and 'opp'
    specified in row.

    :df: A DataFrame representing a game's play-by-play data after
        _clean_features has been called and 'team' and 'opp' have been added by
        _add_team_columns.
    :returns: A dict with new features in addition to previous features.
    """
    assert df.team.notnull().all()

    homeOnOff = df['team'] == df['home']
    # create column for distToGoal
    df['distToGoal'] = np.where(df['team'] != df['fieldSide'],
                                df['ydLine'], 100 - df['ydLine'])
    df['distToGoal'] = np.where(df['isXP'] | df['isTwoPoint'],
                                2, df['distToGoal'])
    # create column for each team's WP
    df['team_wp'] = np.where(homeOnOff, df['home_wp'], 100. - df['home_wp'])
    df['opp_wp'] = 100. - df['team_wp']
    # create columns for each team's WPA
    df['team_wpa'] = np.where(homeOnOff, df['home_wpa'], -df['home_wpa'])
    df['opp_wpa'] = -df['team_wpa']
    # create column for offense and defense scores if not already there
    assert df['boxscore_id'].nunique() == 1
    bs_id = df['boxscore_id'].values[0]
    bs = sportsref.nfl.boxscores.BoxScore(bs_id)
    df['team_score'] = np.where(df['team'] == bs.home(),
                                df['pbp_score_hm'], df['pbp_score_aw'])
    df['opp_score'] = np.where(df['team'] == bs.home(),
                               df['pbp_score_aw'], df['pbp_score_hm'])

    return df
def _add_team_features(df):
    """Adds extra convenience features based on teams with and without
    possession, with the precondition that the there are 'team' and 'opp'
    specified in row.

    :df: A DataFrame representing a game's play-by-play data after
        _clean_features has been called and 'team' and 'opp' have been added by
        _add_team_columns.
    :returns: A dict with new features in addition to previous features.
    """
    assert df.team.notnull().all()

    homeOnOff = df['team'] == df['home']
    # create column for distToGoal
    df['distToGoal'] = np.where(df['team'] != df['fieldSide'],
                                df['ydLine'], 100 - df['ydLine'])
    df['distToGoal'] = np.where(df['isXP'] | df['isTwoPoint'],
                                2, df['distToGoal'])
    # create column for each team's WP
    df['team_wp'] = np.where(homeOnOff, df['home_wp'], 100. - df['home_wp'])
    df['opp_wp'] = 100. - df['team_wp']
    # create columns for each team's WPA
    df['team_wpa'] = np.where(homeOnOff, df['home_wpa'], -df['home_wpa'])
    df['opp_wpa'] = -df['team_wpa']
    # create column for offense and defense scores if not already there
    assert df['boxscore_id'].nunique() == 1
    bs_id = df['boxscore_id'].values[0]
    bs = sportsref.nfl.boxscores.BoxScore(bs_id)
    df['team_score'] = np.where(df['team'] == bs.home(),
                                df['pbp_score_hm'], df['pbp_score_aw'])
    df['opp_score'] = np.where(df['team'] == bs.home(),
                               df['pbp_score_aw'], df['pbp_score_hm'])

    return df
def _add_team_features ( df ) : assert df . team . notnull ( ) . all ( ) homeOnOff = df [ 'team' ] == df [ 'home' ] # create column for distToGoal df [ 'distToGoal' ] = np . where ( df [ 'team' ] != df [ 'fieldSide' ] , df [ 'ydLine' ] , 100 - df [ 'ydLine' ] ) df [ 'distToGoal' ] = np . where ( df [ 'isXP' ] | df [ 'isTwoPoint' ] , 2 , df [ 'distToGoal' ] ) # create column for each team's WP df [ 'team_wp' ] = np . where ( homeOnOff , df [ 'home_wp' ] , 100. - df [ 'home_wp' ] ) df [ 'opp_wp' ] = 100. - df [ 'team_wp' ] # create columns for each team's WPA df [ 'team_wpa' ] = np . where ( homeOnOff , df [ 'home_wpa' ] , - df [ 'home_wpa' ] ) df [ 'opp_wpa' ] = - df [ 'team_wpa' ] # create column for offense and defense scores if not already there assert df [ 'boxscore_id' ] . nunique ( ) == 1 bs_id = df [ 'boxscore_id' ] . values [ 0 ] bs = sportsref . nfl . boxscores . BoxScore ( bs_id ) df [ 'team_score' ] = np . where ( df [ 'team' ] == bs . home ( ) , df [ 'pbp_score_hm' ] , df [ 'pbp_score_aw' ] ) df [ 'opp_score' ] = np . where ( df [ 'team' ] == bs . home ( ) , df [ 'pbp_score_aw' ] , df [ 'pbp_score_hm' ] ) return df
Adds extra convenience features based on teams with and without
    possession, with the precondition that the there are 'team' and 'opp'
    specified in row.

    :df: A DataFrame representing a game's play-by-play data after
        _clean_features has been called and 'team' and 'opp' have been added by
        _add_team_columns.
    :returns: A dict with new features in addition to previous features.
Adds extra convenience features based on teams with and without possession with the precondition that the there are team and opp specified in row .