def update_player_last_totals(last_range): print(f"Fetching player stat totals for last {last_range}...") current_season = get_current_season_full() date_from = get_date_from_last_range(last_range) stats_response = leaguedashplayerstats.LeagueDashPlayerStats( season=current_season, date_from_nullable=date_from) primary_keys = ["PLAYER_NAME"] update_data_table_from_stats_response(f"player_last_{last_range}_totals", stats_response, primary_keys)
def update_player_season(season): full_season = format_season(season) print(f"Fetching player stat totals for {full_season}...") totals_response = leaguedashplayerstats.LeagueDashPlayerStats( season=full_season) print(f"Fetching player stats per 100 for {full_season}...") per_100_response = leaguedashplayerstats.LeagueDashPlayerStats( season=full_season, per_mode_detailed="Per100Possessions") primary_keys = ["SEASON", "PLAYER_NAME"] extra_values = [("SEASON", full_season)] update_data_table_from_stats_response("player_seasons_totals", totals_response, primary_keys, extra_values) update_data_table_from_stats_response("player_seasons_per_100", per_100_response, primary_keys, extra_values)
def by_season(year, debug=False, games_thresh=0, headers=None): ''' given a year, gets all players in a dataframe args: `year`: str: year, like `2019-20` `debug`: bool: if we want to print out dataframe `games_thresh`: int: take out players who player <= `games_thresh` games ''' # given the year, gets all of the players in 1 call p = leaguedashplayerstats.LeagueDashPlayerStats( per_mode_detailed='PerGame', season='2019-20', headers=headers) data = p.league_dash_player_stats.data['data'] # all the columns we want for clustering columns = np.array([ 'PlayerID', 'GP', 'PTS', 'AST', 'REB', 'STL', 'BLK', 'TOV', 'FT_PCT', 'FG_PCT', 'FG3_PCT', 'FTA', 'FGA', 'FG3A', 'MIN', 'PLUS_MINUS' ]) r = len(data) c = len(columns) # store players into matrix with r players (rows) and c columns players_arr = np.zeros((r, c)) # non-empty data if data != []: # these are the indices that are used for the columns, this is what stats.nba.com decided num = np.array( [0, 5, 29, 22, 21, 24, 25, 23, 18, 12, 15, 17, 11, 14, 9, 30]) for i, player in enumerate(data): try: # print if player's name name = players.find_player_by_id(player[0])['full_name'] print(f'{name} found.') except: # name not found print('Player name not found') for j, n in enumerate(num): # populate matrix with data players_arr[i, j] = player[n] # convert matrix and cols to df to store in csv on disk df = pd.DataFrame(players_arr, columns=columns) to_int = ['PlayerID', 'GP'] # only columns that need to be int df[to_int] = df[to_int].astype(int) df_filtered = df[df['GP'] > games_thresh] if debug: print(df_filtered) # also export to csv, so you only have to run it once. df_filtered.to_csv(f'data/{year}_nba_players.csv') return df_filtered
def get_columns_for_measure_type(measure_type): player_stats = leaguedashplayerstats.LeagueDashPlayerStats( last_n_games=0, season='2018-19', measure_type_detailed_defense=measure_type, month=0, opponent_team_id=0, period=0, date_from_nullable='2018-02-01', date_to_nullable='2018-02-01') return player_stats.get_dict()['resultSets'][0]['headers']
def main(): # Get current NBA season full_season = get_current_season_full() # Fetch current season stats (totals and per 100) stats = leaguedashplayerstats.LeagueDashPlayerStats(season=full_season) stats_totals = stats.get_normalized_dict()["LeagueDashPlayerStats"] stats = leaguedashplayerstats.LeagueDashPlayerStats( season=full_season, per_mode_detailed="Per100Possessions") stats_per_100 = stats.get_normalized_dict()["LeagueDashPlayerStats"] # Calculate scoring rating for each player for player_season_totals, player_season_per_100 in zip( stats_totals, stats_per_100): inject_scoring_rating(player_season_totals, player_season_per_100) # Sort players by scoring rating player_seasons_by_scoring_rtg = order_by(stats_totals, ["-SCORING_RATING"]) # Output data print_stats(player_seasons_by_scoring_rtg)
def get_ra_fga_percentchange(): centers = leaguedashplayerstats.LeagueDashPlayerStats( player_position_abbreviation_nullable="C") teams = teams.get_teams() # really just centers who play more than an mpg limit # mpg is calculated here as min / gp starting_centers = [ center for center in centers.data_sets[0].data['data'] if center[9] / center[5] > MINUTES_THRESHOLD ] result = {} # iterate through every combination of starting center and opposing team for center in starting_centers: percent_change_sum = 0 for team in teams: # ignore matchup if center is facing own team (sanity check) if not team['id'] is center[2]: matchup_stats = teamvsplayer.TeamVsPlayer( center[0], team['id']) #check if there were games played / is data if len(matchup_stats.shot_area_on_court.data['data']) == 0: continue games_played = matchup_stats.overall.data['data'][0][4] games_played_matchup = matchup_stats.on_off_court.data['data'][ 0][7] fga = matchup_stats.overall.data['data'][0][10] / games_played fga_oncourt = matchup_stats.on_off_court.data['data'][0][ 13] / games_played_matchup ra_fga = matchup_stats.shot_area_overall.data['data'][0][ 6] / games_played ra_fga_oncourt = matchup_stats.shot_area_on_court.data['data'][ 0][9] / games_played_matchup # percent change of porportion of field goals attempted in # restricted area with center on the court percent_change = ((ra_fga_oncourt / fga_oncourt) - (ra_fga / fga)) / (ra_fga / fga) percent_change_sum += percent_change average_rim_intimidation = percent_change_sum / len(teams) result[center[1]] = average_rim_intimidation return result
def get_player_stats_df( date_from='2018-10-16', date_to=(date.today() - timedelta(1)).strftime('%Y-%m-%d'), team=0, opp_team=0, division=DivisionNullable.default, vs_division=DivisionNullable.default, conference=ConferenceNullable.default, vs_conference=ConferenceNullable.default, period=0, game_segment=GameSegmentNullable.default, home_away=LocationNullable.default, outcome=OutcomeNullable.default, player_position=PlayerPositionAbbreviationNullable.default, player_experience=PlayerExperienceNullable.default, starter_bench=StarterBenchNullable.default, shot_clock_range=ShotClockRangeNullable.default): # Convert team name inputs to ID lists, if applicable if opp_team != 0: opp_team = [team_full_name_to_id(tm) for tm in opp_team] if team != 0: team = [team_full_name_to_id(tm) for tm in team] player_stats = leaguedashplayerstats.LeagueDashPlayerStats( last_n_games=0, season='2018-19', measure_type_detailed_defense='Base', month=0, opponent_team_id=opp_team, period=period, date_from_nullable=date_from, date_to_nullable=date_to, team_id_nullable=team, division_simple_nullable=division, vs_division_nullable=vs_division, conference_nullable=conference, vs_conference_nullable=vs_conference, game_segment_nullable=game_segment, location_nullable=home_away, outcome_nullable=outcome, player_position_abbreviation_nullable=player_position, player_experience_nullable=player_experience, starter_bench_nullable=starter_bench, shot_clock_range_nullable=shot_clock_range) header = player_stats.get_dict()['resultSets'][0]['headers'] data = player_stats.get_dict()['resultSets'][0]['rowSet'] return pd.DataFrame(data=data, columns=header)
def _mode_average_stats(self) -> pd.DataFrame: all_players = leaguedashplayerstats.LeagueDashPlayerStats(last_n_games=self._last_n_games) all_players = all_players.get_data_frames()[0] all_players.loc[:, ['PTS', 'REB', 'AST', 'TOV', 'STL', 'BLK']] = ( all_players.loc[:, ['PTS', 'REB', 'AST', 'TOV', 'STL', 'BLK']].div(all_players.GP, axis=0) ) all_players = all_players.loc[:, ['PLAYER_NAME', 'GP', 'PTS', 'REB', 'AST', 'TOV', 'STL', 'BLK']] all_players = all_players[all_players.GP > 1].reset_index(drop=True) all_players['SCR'] = ( all_players['PTS'] + all_players['REB'] * 1.2 + all_players['AST'] * 1.5 + all_players['STL'] * 3 + all_players['BLK'] * 3 - all_players['TOV'] ) return all_players.round(2)
def get_advanced_stats(): try: advanced_stats = leaguedashplayerstats.LeagueDashPlayerStats( team_id_nullable=miami_heat_team_id, last_n_games=0, measure_type_detailed_defense='Advanced', month=0, opponent_team_id=0, pace_adjust='N', per_mode_detailed='PerGame', period=0, plus_minus='Y', rank='Y', season='2018-19', season_type_all_star='Regular Season', ) advanced_stats = advanced_stats.get_dict() return processnbadict(advanced_stats['resultSets'][0]) #array [] except Exception as e: return str(e)
def run(): #players = leaguedashplayerstats.LeagueDashPlayerStats(season='2020-21',per_mode_detailed="PerGame",season_type_all_star="Regular Season") players = leaguedashplayerstats.LeagueDashPlayerStats( season='2020-21', per_mode_detailed="PerGame", season_type_all_star="Regular Season", player_position_abbreviation_nullable="C") players = players.get_data_frames()[0] for i, j in players.iterrows(): print(j.PLAYER_NAME) """ g = Game(week = row[0], vteam = row[1], hteam = row[2], temp = row[7], wind_mph = row[8], vdflg = row[9], hdflg = row[10], divgame = row[11], nsite = row[12], hospread = row[14], ouopen = row[15], hcspread = row[17], ouclose= row[18], vTOTALDVOA= row[19], vTOTALRNK= row[20], vOFFRNK= row[21], vOFFDVOA= row[22], vDEFRNK= row[23], vDEFDVOA= row[24], vSTRNK= row[25], vSTDVOA= row[26], hTOTALDVOA= row[27], hTOTALRNK= row[28], hOFFRNK= row[29], hOFFDVOA= row[30], hDEFRNK= row[31], hDEFDVOA= row[32], hSTRNK= row[33], hSTDVOA= row[34], vtsw= row[35], vtsl= row[36], vtst= row[37], htsw= row[38], htsl= row[39], htst= row[40], vtw= row[41], vtl= row[42], vtt= row[43], vts= row[44], htw= row[45], htl= row[46], htt= row[47], hts= row[48]) g.save() p = Player(PLAYER_ID = j.PLAYER_ID,Player_Name=j.PLAYER_NAME, Team_ID=j.TEAM_ID, Team_Name=j.TEAM_ABBREVIATION, GP=j.GP, MIN=j.MIN, FGM=j.FGM, FGA=j.FGA, FG_PCT=j.FG_PCT, FG3M=j.FG3M, FG3A=j.FG3A, FG3_PCT=j.FG3_PCT, FTM=j.FTM, FTA=j.FTA, FT_PCT=j.FT_PCT, OREB=j.OREB, DREB=j.DREB, REB=j.REB, AST=j.AST, STL=j.STL, BLK=j.BLK, TOV=j.TOV, PF=j.PF, PTS=j.PTS) obj, created = Player.objects.update_or_create( Player_Name=j.PLAYER_NAME, defaults={'PLAYER_ID' : j.PLAYER_ID, 'Team_ID':j.TEAM_ID, 'Team_Name':j.TEAM_ABBREVIATION, 'GP':j.GP, 'MIN':j.MIN, 'FGM':j.FGM, 'FGA':j.FGA, 'FG_PCT':j.FG_PCT, 'FG3M':j.FG3M, 'FG3A':j.FG3A, 'FG3_PCT':j.FG3_PCT, 'FTM':j.FTM, 'FTA':j.FTA, 'FT_PCT':j.FT_PCT, 'OREB':j.OREB, 'DREB':j.DREB, 'REB':j.REB, 'AST':j.AST, 'STL':j.STL, 'BLK':j.BLK, 'TOV':j.TOV, 'PF':j.PF, 'PTS':j.PTS}, ) try: p = Player.objects.get(PLAYER_ID = j.PLAYER_ID) except Player.DoesNotExist: p = False """ try: p = Player.objects.get(PLAYER_ID=j.PLAYER_ID) except Player.DoesNotExist: p = False if p: p.Pos.add(Positions.objects.get(id=3)) p.save()
teams = [] for teamNum in range(30): teams.append(teams_dict["data"][teamNum]) #get rosters rosters = [] for team in teams: time.sleep(1) rosters_dict = commonteamroster.CommonTeamRoster(season="2018-19", team_id=team[1]) rosters_dict = rosters_dict.common_team_roster.get_dict() rosters.append(rosters_dict["data"]) #get playerstats leaguestats = leaguedashplayerstats.LeagueDashPlayerStats( per_mode_detailed="PerGame", season="2018-19") leaguestats = leaguestats.league_dash_player_stats.get_dict() leaguestats = leaguestats["data"] leaguestats.sort(key=lambda x: x[0]) leaguestats_length = len(leaguestats) - 1 sql = "CREATE TABLE IF NOT EXISTS playergamelog (seasonid INT, playerid INT, gameid INT, gamedate DATE, matchup VARCHAR(255), win VARCHAR(255),min INT," sql += "fgm INT, fga INT, fg_pct DECIMAL(4,3), fg3m INT, fg3a INT, fg3_pct DECIMAL(4,3), ftm INT, fta INT, ft_pct DECIMAL(4,3), oreb INt, dreb INT, reb INT," sql += "ast INT, stl INT, blk INT, tov INT, pf INT, pts INT, plusminus INT, PRIMARY KEY (playerid, gamedate))" cursor.execute(sql) for player in leaguestats: player_stats = playergamelog.PlayerGameLog(player_id=player[0], season="2018-19") time.sleep(2) player_stats = player_stats.player_game_log.get_data_frame()
return teams.find_teams_by_full_name(team_full_name)[0]['id'] """================================================================================================================== # Determine list utlization by endpoint ==================================================================================================================""" team_list = ['Philadelphia 76ers', 'Toronto Raptors'] team_id_list = [team_full_name_to_id(tm) for tm in team_list] team_id_list player_stats = leaguedashplayerstats.LeagueDashPlayerStats( last_n_games=0, season='2018-19', measure_type_detailed_defense='Base', month=0, opponent_team_id=0, period=0, date_from_nullable='2019-01-01', date_to_nullable='2019-01-31', team_id_nullable=team_id_list) header = player_stats.get_dict()['resultSets'][0]['headers'] data = player_stats.get_dict()['resultSets'][0]['rowSet'] pd.DataFrame(data=data, columns=header).sample(10) # Looks like if list is passed, only first value passed gets pulled from endpoint """================================================================================================================== # Create UDF that returns data given dimensions (to be made available in dashboard) ==================================================================================================================""" from datetime import date, timedelta yesterday = (date.today() - timedelta(1)).strftime('%Y-%m-%d') print(yesterday)
def players_stats_from(end_yr): return leaguedashplayerstats.LeagueDashPlayerStats( measure_type_detailed_defense='Advanced', season=format_year(end_yr)).get_data_frames()[0]