def compare_matchup(game, event, team):
    gpbp = playbyplay.PlayByPlay(game_id=game[0], headers=headers)
    playByPlay = gpbp.get_data_frames()[0]
    home, visitor = get_home_and_away_event(event.value, playByPlay)
    ## If its a home game for the team
    if game[1]:
        plot_game_fgs(str(event), (team, team_to_color[team], home), (game[2], team_to_color[game[2]], visitor))
    else:
        plot_game_fgs(str(event), (game[2], team_to_color[game[2]], home), (team, team_to_color[team], visitor))
Exemplo n.º 2
0
def get_playbyplay(team_abb=None, season=None, season_segment=None):
        # call get_teams method and store as data frame
        # filter df based on team_abb argument and collect teamid
        nba_teams_df = get_team_info()
        team_ids = list(nba_teams_df[nba_teams_df['abbreviation'].isin(team_abb)]['id'])
        
        # collect game_ids for each team
        game_id_list = get_game_ids(team_id = team_ids, season = season, season_type = season_segment)

        # collect game info for each team
        game_info_df = get_game_info(team_id = team_ids, season = season, season_type = season_segment)

        # Start an empty dict
        pbp_data_dict = {}
        
        # Call PlayByPlay for every game_id. Store in pbp_data_dict
        for game_id in game_id_list:
                df = playbyplay.PlayByPlay(game_id).get_data_frames()[0]
                pbp_data_dict[game_id] = df
                sleep(1)

        # Get dict keys
        dict_keys = pbp_data_dict.keys()
        # Create list of pbp data frames 
        frames = [pbp_data_dict[key] for key in dict_keys]
        # Union the data frame in frames
        pbp_df = pd.concat(frames, axis=0)

        # Convert dtypes
        pbp_df['GAME_ID'] = pbp_df['GAME_ID'].astype(str)
        pbp_df['EVENTNUM'] = pbp_df['EVENTNUM'].astype(str).astype(int)
        pbp_df['EVENTMSGTYPE'] = pbp_df['EVENTMSGTYPE'].astype(str).astype(int)
        pbp_df['EVENTMSGACTIONTYPE'] = pbp_df['EVENTMSGACTIONTYPE'].astype(str).astype(int)
        pbp_df['PERIOD'] = pbp_df['PERIOD'].astype(str).astype(int)
        pbp_df['PCTIMESTRING'] = pbp_df['PCTIMESTRING'].astype(str)
        pbp_df['HOMEDESCRIPTION'] = pbp_df['HOMEDESCRIPTION'].astype(str)
        pbp_df['NEUTRALDESCRIPTION'] = pbp_df['NEUTRALDESCRIPTION'].astype(str)
        pbp_df['VISITORDESCRIPTION'] = pbp_df['VISITORDESCRIPTION'].astype(str)
        pbp_df['SCORE'] = pbp_df['SCORE'].astype(str)
        pbp_df['SCOREMARGIN'] = pbp_df['SCOREMARGIN'].astype(str) ## can't convert to int without dealing with None values

        # Add date to pbp_df
        pbp_df = pbp_df.merge(game_info_df[['GAME_ID', 'GAME_DATE']], on=['GAME_ID'], how='inner')

        # Add home/away column

        # Add column to ID which team is on offense

        # Add event message descriptions to df
        pbp_df['EVENTMSGTYPE_DESC'] = pbp_df['EVENTMSGTYPE'].apply(event_msg_type_desc)

        # Add event action description 
        #pbp_df['EVENTMSGACTION_DESC'] = pbp.df['EVENTMSGACTIONTYPE'].apply(event_mgs_action_desc)

        # Return
        return(pbp_df)
Exemplo n.º 3
0
def get_play_by_play_stats(quarter, game):

    turnovers = 0
    three_attempt = 0
    three_make = 0
    try:
        pbp = playbyplay.PlayByPlay(game['GAME_ID'],
                                    start_period=quarter,
                                    end_period=quarter)
    except:
        print("ERROR")
        print(quarter)
        print(game)
        return (0, 0, 0, 0, 0)
    time.sleep(2)
    dic = pbp.get_normalized_dict()['PlayByPlay']
    if len(dic) == 0:
        return (0, 0, 0, 0, 0)
    try:
        for x in dic:
            if x['VISITORDESCRIPTION'] != None:

                if 'Turnover' in x['VISITORDESCRIPTION']:
                    turnovers += 1

                elif '3PT' in x['VISITORDESCRIPTION']:
                    three_attempt += 1
                    if 'MISS' not in x['VISITORDESCRIPTION']:
                        three_make += 1

            if x['HOMEDESCRIPTION'] != None:

                if 'Turnover' in x['HOMEDESCRIPTION']:
                    turnovers += 1

                elif '3PT' in x['HOMEDESCRIPTION']:
                    three_attempt += 1
                    if 'MISS' not in x['HOMEDESCRIPTION']:
                        three_make += 1

        i = len(dic) - 1
        while dic[i]['SCORE'] == None:
            i -= 1
        away_final = int(dic[i]['SCORE'].split("-")[0])
        home_final = int(dic[i]['SCORE'].split("-")[1])
        return (turnovers, three_make, three_attempt, away_final + home_final,
                abs(away_final - home_final))
    except:
        print("ERROR")
        print(game)
        print(dic)
Exemplo n.º 4
0
def get_stats(team_abbr):

	'''
	This function will return the last game stats of a particlar team.

	Parameters:
	team_abbr: pass a string with the abbreviation of a particular team

	example> get_stats('CHI')
	'''

	nba_teams = teams.get_teams()


	# Select the dictionary for the Pacers, which contains their team ID
	nba_team = [team for team in nba_teams if team['abbreviation'] == team_abbr][0]
	nba_team_id = nba_team['id']

	# Query for the last regular season game where the Pacers were playing

	gamefinder = leaguegamefinder.LeagueGameFinder(team_id_nullable=nba_team_id,
	                            season_nullable=Season.default,
	                            season_type_nullable=SeasonType.regular)  

	games_dict = gamefinder.get_normalized_dict()
	games = games_dict['LeagueGameFinderResults']
	game = games[0]
	game_id = game['GAME_ID']
	game_matchup = game['MATCHUP']

	print(f'Searching through {len(games)} game(s) for the game_id of {game_id} where {game_matchup}')

	# Query for the play by play of that most recent regular season game
	df = playbyplay.PlayByPlay(game_id).get_data_frames()[0]
	df.head() #just looking at the head of the data

	df.to_csv(team_abbr+'_'+str(game_id)+'.csv')

	return print('stats have been exported in the working folder!')
Exemplo n.º 5
0
def lastleadchange_create_df(season,
                             cat,
                             pickle_flag=0,
                             plot_flag=0,
                             tocsv_flag=0):

    if pickle_flag == 1:

        league_gamelog, gameid_all_list = create_cat_list(season, cat)

        wl = 'W'
        wl_gamelog = league_gamelog[league_gamelog['WL'] == wl]

        gameid_list = wl_gamelog['GAME_ID'].tolist()
        team_log_list = wl_gamelog['TEAM_NAME'].tolist()
        teamabb_log_list = wl_gamelog['TEAM_ABBREVIATION'].tolist()

        lastleadchange_period_list = []
        lastleadchange_time_list = []

        for game in gameid_list:

            print(game)

            response_pbp = pbp.PlayByPlay(game_id=game)

            time.sleep(0.5)

            content_pbp = json.loads(response_pbp.get_json())
            results_pbp = content_pbp['resultSets'][0]
            headers_pbp = results_pbp['headers']
            rows_pbp = results_pbp['rowSet']
            league_pbp = pd.DataFrame(rows_pbp)
            league_pbp.columns = headers_pbp

            league_pbp.loc[league_pbp['SCOREMARGIN'] == 'TIE',
                           'SCOREMARGIN'] = 0
            # league_pbp.loc[league_pbp['SCOREMARGIN'] != 'TIE', 'Tied?'] = 'False'

            league_pbp = league_pbp[league_pbp['SCOREMARGIN'].notnull()]

            league_pbp['SCOREMARGIN'] = league_pbp['SCOREMARGIN'].astype(int)

            if league_pbp['SCOREMARGIN'].iat[-1] < 0:

                league_pbp.loc[league_pbp['SCOREMARGIN'] > 0,
                               'Loser Lead or Tie?'] = 'True'
                league_pbp.loc[league_pbp['SCOREMARGIN'] == 0,
                               'Loser Lead or Tie?'] = 'True'

            else:

                league_pbp.loc[league_pbp['SCOREMARGIN'] < 0,
                               'Loser Lead or Tie?'] = 'True'
                league_pbp.loc[league_pbp['SCOREMARGIN'] == 0,
                               'Loser Lead or Tie?'] = 'True'

            league_pbp.reset_index(drop=True, inplace=True)

            lastleadchange_idx = league_pbp[league_pbp['Loser Lead or Tie?'] ==
                                            'True'].last_valid_index()

            if lastleadchange_idx is not None and lastleadchange_idx != len(
                    league_pbp):

                lastleadchange_period_list.append(
                    league_pbp['PERIOD'][lastleadchange_idx + 1])
                lastleadchange_time_list.append(
                    league_pbp['PCTIMESTRING'][lastleadchange_idx + 1])

            elif lastleadchange_idx == len(league_pbp):

                lastleadchange_period_list.append(
                    league_pbp['PERIOD'][lastleadchange_idx])
                lastleadchange_time_list.append(
                    league_pbp['PCTIMESTRING'][lastleadchange_idx])

            else:

                lastleadchange_period_list.append(league_pbp['PERIOD'][0])
                lastleadchange_time_list.append(league_pbp['PCTIMESTRING'][0])

        lastleadchange_df = pd.DataFrame(
            list(
                zip(gameid_list, team_log_list, teamabb_log_list,
                    lastleadchange_period_list, lastleadchange_time_list)),
            columns=['GAME_ID', 'TEAM', 'TEAM_ABB', 'PERIOD', 'TIME'])

        file_name = './lastleadchange_' + season + '.pkl'

        lastleadchange_df.to_pickle(file_name)

    else:

        file_name = './lastleadchange_' + season + '.pkl'
        lastleadchange_df = pd.read_pickle(file_name)

    longest_game = 48 + (lastleadchange_df['PERIOD'].max() - 4) * 5

    period_list = lastleadchange_df['PERIOD'].tolist()

    period_list = [
        period * 12 if period <= 4 else period * 5 + 48
        for period in period_list
    ]

    time_split = lastleadchange_df['TIME'].str.split(':', expand=True)

    lastleadchange_df['MIN'] = time_split[0].astype(int)
    lastleadchange_df['SEC'] = time_split[1].astype(int)
    lastleadchange_df['ELAPSED TIME'] = period_list - lastleadchange_df[
        'MIN'] - lastleadchange_df['SEC'] / 60

    team_gb_mean = lastleadchange_df.groupby(['TEAM_ABB']).mean()
    team_gb_mean.sort_values(by='ELAPSED TIME', inplace=True, ascending=False)

    if plot_flag == 1:

        plt.style.use('Solarize_Light2')
        name_list, abb_list, primary1_list = team_colors.return_teamcolors_lists(
        )
        colors_df = pd.DataFrame(list(zip(abb_list, primary1_list)),
                                 columns=['TEAM_ABB', 'COLOR_1'])

        team_gb_mean = pd.merge(team_gb_mean,
                                colors_df,
                                on=['TEAM_ABB'],
                                how='left').set_index('TEAM_ABB')
        fig_box, ax_box = plt.subplots()
        box_team = lastleadchange_df.boxplot(column='ELAPSED TIME',
                                             by='TEAM_ABB',
                                             ax=ax_box,
                                             vert=False)

        fig_bar, ax_bar = plt.subplots()

        q_labels = ['Q1', 'Q2', 'Q3', 'Q4']
        qtime_labels = [0, 12, 24, 36]
        plt.xticks(ticks=qtime_labels, labels=q_labels)
        plt.yticks(fontsize=6)

        plt.barh(y=team_gb_mean.index,
                 width=team_gb_mean['ELAPSED TIME'],
                 color=team_gb_mean['COLOR_1'])
        LCfont = {'fontname': 'Lucida Console'}

        plt.suptitle('if the Jazz are ahead at halftime, maybe give up',
                     **LCfont,
                     fontsize=16)
        plt.title(
            'AVERAGE TIME IN WHICH TEAM TAKES & KEEPS LEAD | 2020-21, through May 10 | @cale_williams',
            **LCfont,
            fontsize=6,
            loc='left')
        plt.axvline(x=qtime_labels[1], ls=':', lw=1.5, color='black')
        plt.axvline(x=qtime_labels[2], ls=':', lw=1.5, color='black')
        plt.axvline(x=qtime_labels[3], ls=':', lw=1.5, color='black')

        plt.savefig('lastleadchange_2020-21.jpeg',
                    dpi=500,
                    bbox_inches='tight')

        plt.show()

    # print(lastleadchange_df)

    if tocsv_flag == 1:

        lastleadchange_df.to_csv('./lastleadchange_2020-21.csv')
        team_gb_mean.to_csv('./team_gb_med_2020-21.csv')

    return lastleadchange_df
# find team Ids
from nba_api.stats.static import teams 
teams = teams.get_teams()
GSW = [x for x in teams if x['full_name'] == 'Golden State Warriors'][0]
GSW_id = GSW['id']

#game_stats_player
#could not get DateFrom to work, just use loop with years if necessary 
from nba_api.stats.library.parameters import SeasonAll
from nba_api.stats.endpoints import playergamelog
import pandas as pd 

gamelog_bron = playergamelog.PlayerGameLog(player_id='2544', season = '2018')
df_bron_games_2018 = gamelog_bron.get_data_frames()

gamelog_bron_all = playergamelog.PlayerGameLog(player_id='2544', season = SeasonAll.all)
df_bron_games_all = gamelog_bron_all.get_data_frames()

#find games played by a team or player
from nba_api.stats.endpoints import leaguegamefinder
GSW_games = leaguegamefinder.LeagueGameFinder(team_id_nullable=GSW_id).get_data_frames()[0]

# game play by play data 
from nba_api.stats.endpoints import playbyplay
pbp = playbyplay.PlayByPlay('0021900429').get_data_frames()[0]





Exemplo n.º 7
0
kareem = playercareerstats.PlayerCareerStats(player_id="76003")

kareem.get_data_frames()[0]

### get all games from a team (current season, Atlanta 1610612737)
from nba_api.stats.endpoints import teamgamelog

gamelog = teamgamelog.TeamGameLog(team_id="1610612737")

gamelog.get_data_frames()[0]

## get play by play from desired game (0021900543 ATL vs DEN)
from nba_api.stats.endpoints import playbyplay

gamepbp = playbyplay.PlayByPlay(game_id="0021900543")

gamepbp.get_data_frames()[0].sort_values("EVENTNUM")

## get traditional box score from the same game
from nba_api.stats.endpoints import boxscoretraditionalv2

boxscore = boxscoretraditionalv2.BoxScoreTraditionalV2(game_id="0021900543")

boxscore.get_data_frames()[0]

## based on teamgamelog and box score, we could calculate the FPPM for each player
## but how to link games to players? have to go through team?

from nba_api.stats.endpoints import playergamelog
def getPbpCSV(gameid):
    return playbyplay.PlayByPlay(gameid).get_data_frames()[0].to_csv(
        './game_data/playbyplay.csv')
Exemplo n.º 9
0
gamefinder = leaguegamefinder.LeagueGameFinder(
    team_id_nullable=pacers_id,
    season_nullable=Season.default,
    season_type_nullable=SeasonType.regular)

games_dict = gamefinder.get_normalized_dict()
games = games_dict['LeagueGameFinderResults']
game = games[0]
game_id = game['GAME_ID']
game_matchup = game['MATCHUP']

print(
    f'Searching through {len(games)} game(s) for the game_id of {game_id} where {game_matchup}'
)

# Query for the play by play of that most recent regular season game
from nba_api.stats.endpoints import playbyplay
df = playbyplay.PlayByPlay(game_id).get_data_frames()[0]
df.head()  #just looking at the head of the data

from nba_api.stats.endpoints import shotchartdetail, shotchartlineupdetail, playerdashptshots, playercareerstats

shot_df = shotchartdetail.ShotChartDetail(
    team_id=mia_id, player_id='1628389').get_data_frames()[0]

player_career = playercareerstats.PlayerCareerStats(
    player_id='1628389').get_data_frames()[0]

shotchartlineupdetail.ShotChartLineupDetail()