def nets_player_plus_minus_by_year(player_id): id = player_id player_year_info = player.PlayerYearOverYearSplits(id).by_year() plus_minus = "N/A" for info in player_year_info: if info["GROUP_VALUE"] == "2017-18": plus_minus = info["PLUS_MINUS"] else: continue return plus_minus
def getPlayerStats(self, measure_type="Base"): yoy = player.PlayerYearOverYearSplits(measure_type=measure_type, player_id=self.player_id) json = yoy.json total = [] header = self.__trimData__(measure_type, json["resultSets"][0]["headers"]) for yeardata in json["resultSets"][1]["rowSet"]: year = yeardata[-1] yeardata = self.__trimData__(measure_type, yeardata) total.append((year, yeardata)) self.stats = dict(total) self.header = header self.projected_salaries = self.getProjectedSalary() return total
def test(): pid = get_player('Tim', 'Duncan') vs_pid = get_player('Stephen', 'Curry') assert player.PlayerList() assert player.PlayerSummary(pid) # assert player.PlayerGeneralSplits(pid) # assert player.PlayerOpponentSplits(pid) assert player.PlayerLastNGamesSplits(pid) assert player.PlayerInGameSplits(pid) assert player.PlayerClutchSplits(pid) # assert player.PlayerShootingSplits(pid) assert player.PlayerPerformanceSplits(pid) assert player.PlayerYearOverYearSplits(pid) assert player.PlayerCareer(pid) assert player.PlayerProfile(pid) assert player.PlayerGameLogs(pid) assert player.PlayerShotTracking(pid) assert player.PlayerReboundTracking(pid) assert player.PlayerPassTracking(pid) assert player.PlayerDefenseTracking(pid) # assert player.PlayerShotLogTracking(pid) # assert player.PlayerReboundLogTracking(pid) assert player.PlayerVsPlayer(pid, vs_pid)
def testAll(self): assert player.PlayerList() assert player.PlayerSummary(self.playerId) # assert player.PlayerGeneralSplits(self.playerId) # assert player.PlayerOpponentSplits(self.playerId) assert player.PlayerLastNGamesSplits(self.playerId) assert player.PlayerInGameSplits(self.playerId) assert player.PlayerClutchSplits(self.playerId) # assert player.PlayerShootingSplits(self.playerId) assert player.PlayerPerformanceSplits(self.playerId) assert player.PlayerYearOverYearSplits(self.playerId) assert player.PlayerCareer(self.playerId) assert player.PlayerProfile(self.playerId) assert player.PlayerGameLogs(self.playerId) assert player.PlayerShotTracking(self.playerId) assert player.PlayerReboundTracking(self.playerId) assert player.PlayerPassTracking(self.playerId) assert player.PlayerDefenseTracking(self.playerId) # assert player.PlayerShotLogTracking(self.playerId) # assert player.PlayerReboundLogTracking(self.playerId) assert player.PlayerVsPlayer(self.playerId, self.vs_playerId)
def get_fantasy_points_by_player(player_first_name, player_last_name): player_stats = None print("Loading: Getting Fantasy Point for " + player_first_name + " " + player_last_name) try: player_id = player.get_player(player_first_name, player_last_name, CURRENT_SEASON, 0, True) player_stats = player.PlayerYearOverYearSplits( player_id, 0, "Base", "PerGame", "N", "N", "N", League.Default, CURRENT_SEASON, 'Regular Season', "0", "", "", "0", "", '', '', '0', '', '', '', '0', '', '0').overall()[0] except StopIteration: print("Player not found") except IndexError: print(0) return 0 fgm = player_stats["FGM"] fgmi = -0.5 * (player_stats["FGA"] - player_stats["FGM"]) ftm = player_stats["FTM"] ftmi = -0.5 * (player_stats["FTA"] - player_stats["FTM"]) fg3m = player_stats["FG3M"] reb = player_stats["REB"] ast = player_stats["AST"] stl = player_stats["STL"] blk = player_stats["BLK"] to = -0.25 * (player_stats["TOV"]) pts = player_stats["PTS"] fantasy_points = round( fgm + fgmi + ftm + ftmi + fg3m + reb + ast + stl + blk + to + pts, 1) print(fantasy_points) return fantasy_points
def extract_data(player_id): player_object = player.PlayerYearOverYearSplits(player_id) player_data = player_object.by_year() rookie_data = player_data.tail(n=1)
def generate_overalls_df(player_id_lst, year): lst_of_dicts = [] for id in player_id_lst: print id stats = player.PlayerYearOverYearSplits(id).by_year() stats = stats[stats.GROUP_VALUE == year] if not stats.empty: gp = float(stats.GP) min_game = float(stats.MIN) ftm = float(stats.FTM) fta = float(stats.FTA) oreb = float(stats.OREB) dreb = float(stats.DREB) reb = float(stats.REB) ast = float(stats.AST) tov = float(stats.TOV) stl = float(stats.STL) blk = float(stats.BLK) blk_a = float(stats.BLKA) pfd = float(stats.PFD) pf = float(stats.PF) lst_of_dicts.append({ 'player_id': str(id), 'gp': gp, 'min_game': min_game, 'ftm': ftm, 'fta': fta, 'oreb': oreb, 'dreb': dreb, 'reb': reb, 'ast': ast, 'tov': tov, 'stl': stl, 'blk': blk, 'blk_a': blk_a, 'pfd': pfd, 'pf': pf }) # time.sleep(1) else: lst_of_dicts.append({ 'player_id': str(id), 'gp': 0, 'min_game': 0, 'ftm': 0, 'fta': 0, 'oreb': 0, 'dreb': 0, 'reb': 0, 'ast': 0, 'tov': 0, 'stl': 0, 'blk': 0, 'blk_a': 0, 'pfd': 0, 'pf': 0 }) # time.sleep(1) overalls_df = pd.DataFrame(lst_of_dicts) overalls_df.set_index('player_id', inplace=True, drop=True) return overalls_df
def __init__(self, playerId, teamId, paceAdjustment, position=None, salary=0, freeAgent=False): self.playerId = playerId self.teamId = teamId self.paceAdjustment = paceAdjustment # Access stats from NBA Stats through nba_py module summary = player.PlayerSummary(playerId) info = summary.info()[0] header = player.PlayerSummary(playerId).headline_stats()[0] # Determine age in years from birthdate given in profile # NOTE: Age is current age today, not during this season bday = info['BIRTHDATE'] date = datetime.date(int(bday[:4]), int(bday[5:7]), int(bday[8:10])) self.age = (datetime.date.today() - date).days / 365.0 years = player.PlayerYearOverYearSplits(playerId).by_year() i = 0 # find stats from the current season, if stats incomplete for that season return # an earlier season while (str(years[i]['GROUP_VALUE']) != constants.SEASON and (i < len(years) - 1)): i += 1 if (i == len(years)): pc = years[len(years) - 1] else: pc = years[i] self.isFreeAgent = freeAgent self.name = str(header['PLAYER_NAME']) if (playerId in constants.HUSTLE.keys()): (sa, df, lbr, cd, cs) = constants.HUSTLE[playerId] else: (sa, df, lbr, cd, cs) = constants.HUSTLE_AVG self.sa = sa # screen assists self.df = df # deflections self.lbr = lbr # loose balls recovered self.cd = cd # charges drawn self.cs = cs # contested shots self.tp = pc['FG3M'] * 1.0 # three points made self.mp = pc['MIN'] * 1.0 # minutes played self.ast = pc['AST'] * 1.0 # assists self.fg = pc['FGM'] * 1.0 # field goals self.tov = pc['TOV'] * 1.0 # turnovers self.fga = pc['FGA'] * 1.0 # field goals attempted self.fta = pc['FTA'] * 1.0 # free throws attempted self.ft = pc['FTM'] * 1.0 # free throws made self.stl = pc['STL'] * 1.0 # steals self.orb = pc['OREB'] * 1.0 # offensive rebounds self.blk = pc['BLK'] * 1.0 # blocks self.pf = pc['PF'] * 1.0 # personal fouls self.pts = pc['PTS'] * 1.0 # points self.trb = pc['REB'] * 1.0 # total rebounds self.drb = pc['DREB'] * 1.0 # defensive rebounds self.salary = salary # cost of player if (position == None): self.position = str(info['POSITION']) # position of player else: self.position = position
def get_player_stat(player_id): """ Takes player NBA ID and returns a data frame containing player performance data such as shooting, blocking, rebound etc... """ # Extracting shooting and blocking columns player_shot_block_raw = player.PlayerShootingSplits(player_id, season='2018-19').shot_areas() if player_shot_block_raw.shape[0] == 0: raise NoDataError("No data for {}".format(player_id)) player_shot_and_block_by_area = player_shot_block_raw.T player_shot_and_block_by_area.columns = player_shot_and_block_by_area.loc['GROUP_VALUE'] player_shot_and_block_by_area = player_shot_and_block_by_area.loc[[ 'FGA', 'BLKA'], "Restricted Area":"Above the Break 3"] player_shot_by_area = player_shot_and_block_by_area.loc[["FGA"], :] player_block_by_area = player_shot_and_block_by_area.loc[["BLKA"], :] # Use a dict to map old col name to new col name, using dict is required # because the API sometimes returns inconsistent column order and number # of cols shot_col_map = {"Restricted Area": 'shot_res', "In The Paint (Non-RA)": 'shot_in_paint', "Mid-Range": 'shot_mid_range', 'Left Corner 3': 'shot_lcorner_3', 'Right Corner 3': 'shot_rcorner_3', 'Above the Break 3': 'shot_above_3'} block_col_map = {"Restricted Area": 'block_res', "In The Paint (Non-RA)": 'block_in_paint', "Mid-Range": 'block_mid_range', 'Left Corner 3': 'block_lcorner_3', 'Right Corner 3': 'block_rcorner_3', 'Above the Break 3': 'block_above_3'} # Applying the column name map player_shot_by_area = player_shot_by_area.rename(columns=shot_col_map) player_block_by_area = player_block_by_area.rename(columns=block_col_map) # Total attempted shots and blocks total_shots = player_shot_by_area.loc['FGA', :].sum() total_blocks = player_block_by_area.loc['BLKA', :].sum() # Normalize all stats by total shots to get relative frequencies for each # player player_shot_by_area /= total_shots + 1e-5 player_block_by_area /= total_blocks + 1e-5 # Record total attempted shots and blocks player_shot_by_area['fga'] = total_shots player_block_by_area['blka'] = total_blocks # PlayerYearOverYearSplits is the end point we will be calling # to get player stats aside from shooting and blocking # Take the most recent 2 years, and then add them rebound_assist = player.PlayerYearOverYearSplits( player_id, per_mode="Per48").by_year().iloc[:2, :] cols_of_interest = ['OREB', 'DREB', 'AST', 'STL', "MIN"] rebound_assist = rebound_assist[cols_of_interest] rebound_assist.columns = [c.lower() for c in rebound_assist.columns] rebound_assist_summed = pd.DataFrame(rebound_assist.sum()).T # Finally, concatnate the dataframes together player_stat = pd.concat([player_shot_by_area.reset_index(drop=True), player_block_by_area.reset_index(drop=True), rebound_assist_summed.reset_index(drop=True)], axis=1) player_stat = player_stat.rename({0: player_id}) return player_stat