def generate_shots_fromNBA(first, last, season): """ Takes first, last, and season as string, returns pandas structure of all shots """ player_id = get_player(first, last) player = shotchart.ShotChart(player_id, season=season) shots = player.shot_chart() return shots
def get_data(): #Lebron is a place holder, first, last and year will be inputs int the future first = 'Lebron' last = 'James' year = '2012-13' player_id = get_player(first, last) #ShotChart creates a readable dictionary of all the data Lebron_James = shotchart.ShotChart(player_id, season=year) shots = Lebron_James.shot_chart() return shots
def league_average(season=None, first='Kevin', last='Durant'): """ Generates the leage averages by zone for the given season default value for first/last name is Kevin Durant, but any player could be used to initialize the ShotChart class from nba_py takes season as a string and defaults to current season returns: Pandas DataFrame """ player = shotchart.ShotChart('201142', season=season) league_average = player.league_average() return league_average
def unique_shots(id_lst): # id_lst = ['201935', '201142', '202326', '2544', '203081'] lst = [] for i in id_lst: chart = shotchart.ShotChart(i, season='2015-16').shot_chart() chart['SHOT_GRP'] = chart['SHOT_TYPE'] + '_' + chart['ACTION_TYPE'] lst.append(chart['SHOT_GRP'].unique()) # lst2 = [shot_type for item in lst for shot_type in item] unique_shots = set(lst2) return unique_shots
def generate_player_shot_loc_df(player_id_list, year): lst_of_dicts = [] for id in player_id_list: print id #get the shotchart for player shot_chart = shotchart.ShotChart(id, season=year).shot_chart() shots = shot_chart[[ 'SHOT_ZONE_BASIC', 'SHOT_ATTEMPTED_FLAG', 'SHOT_MADE_FLAG' ]].groupby('SHOT_ZONE_BASIC').sum() shots.reset_index(inplace=True) if not shots.empty: attempt_RA = shots.SHOT_ATTEMPTED_FLAG[shots.SHOT_ZONE_BASIC == 'Restricted Area'].sum() made_RA = shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'Restricted Area'].sum() attempt_paint = shots.SHOT_ATTEMPTED_FLAG[ shots.SHOT_ZONE_BASIC == 'In The Paint (Non-RA)'].sum() made_paint = shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'In The Paint (Non-RA)'].sum() attempt_mid = shots.SHOT_ATTEMPTED_FLAG[shots.SHOT_ZONE_BASIC == 'Mid-Range'].sum() made_mid = shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'Mid-Range'].sum() attempt_corner_3 = (shots.SHOT_ATTEMPTED_FLAG[shots.SHOT_ZONE_BASIC == 'Left Corner 3'].sum()) + \ (shots.SHOT_ATTEMPTED_FLAG[shots.SHOT_ZONE_BASIC == 'Right Corner 3'].sum()) made_corner_3 = (shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'Left Corner 3'].sum()) + \ (shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'Right Corner 3'].sum()) attempt_non_corner_3 = (shots.SHOT_ATTEMPTED_FLAG[shots.SHOT_ZONE_BASIC == 'Above the Break 3'].sum()) + \ (shots.SHOT_ATTEMPTED_FLAG[shots.SHOT_ZONE_BASIC == 'Backcourt'].sum()) made_non_corner_3 = (shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'Above the Break 3'].sum()) + \ (shots.SHOT_MADE_FLAG[shots.SHOT_ZONE_BASIC == 'Backcourt'].sum()) lst_of_dicts.append({ 'player_id': str(id), 'attempt_RA': attempt_RA, 'made_RA': made_RA, 'attempt_paint': attempt_paint, 'made_paint': made_paint, 'attempt_corner_3': attempt_corner_3, 'made_corner_3': made_corner_3, 'attempt_non_corner_3': attempt_non_corner_3, 'made_non_corner_3': made_non_corner_3, 'attempt_mid': attempt_mid, 'made_mid': made_mid }) else: lst_of_dicts.append({ 'player_id': str(id), 'attempt_RA': 0, 'made_RA': 0, 'attempt_paint': 0, 'made_paint': 0, 'attempt_corner_3': 0, 'made_corner_3': 0, 'attempt_non_corner_3': 0, 'made_non_corner_3': 0, 'attempt_mid': 0, 'made_mid': 0 }) player_shot_loc_df = pd.DataFrame(lst_of_dicts) player_shot_loc_df.set_index('player_id', inplace=True, drop=True) return player_shot_loc_df
def generate_player_shot_df(player_id_list, year): lst_of_dicts = [] for id in player_id_list: print id #get the shotchart for player shot_type = shotchart.ShotChart(id, per_mode='PerPossession', season=year).shot_chart() if not shot_type.empty: shots = shot_type[['ACTION_TYPE','SHOT_TYPE','SHOT_ATTEMPTED_FLAG','SHOT_MADE_FLAG']] \ .groupby(['ACTION_TYPE','SHOT_TYPE']).sum().reset_index() shots['SHOT_GRP'] = shots['SHOT_TYPE'] + '_' + shots['ACTION_TYPE'] #define what we want to add to the df attempt_3 = shots.loc[shots['SHOT_TYPE'] == '3PT Field Goal', 'SHOT_ATTEMPTED_FLAG'].sum() attempt_2 = shots.loc[shots['SHOT_TYPE'] == '2PT Field Goal', 'SHOT_ATTEMPTED_FLAG'].sum() made_3 = shots.loc[shots['SHOT_TYPE'] == '3PT Field Goal', 'SHOT_MADE_FLAG'].sum() made_2 = shots.loc[shots['SHOT_TYPE'] == '2PT Field Goal', 'SHOT_MADE_FLAG'].sum() total_attempt = attempt_3 + attempt_2 total_made = made_3 + made_2 attempt_drive_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Driving Bank Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Finger Roll Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Floating Bank Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Floating Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Reverse Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Reverse Layup Shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_drive_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Driving Bank Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Finger Roll Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Floating Bank Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Floating Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Reverse Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Reverse Layup Shot'), \ 'SHOT_MADE_FLAG'].sum() attempt_at_rim_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Tip Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Putback Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Putback Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Reverse Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Reverse Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Tip Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Alley Oop Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Alley Oop Layup shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Finger Roll Layup Shot'),\ 'SHOT_ATTEMPTED_FLAG'].sum() made_at_rim_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Tip Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Putback Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Putback Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Reverse Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Reverse Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Tip Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Alley Oop Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Alley Oop Layup shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Finger Roll Layup Shot'),\ 'SHOT_MADE_FLAG'].sum() attempt_cut_run_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Running Alley Oop Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Alley Oop Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Finger Roll Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Cutting Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Cutting Finger Roll Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Cutting Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Reverse Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Reverse Layup Shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_cut_run_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Running Alley Oop Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Alley Oop Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Finger Roll Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Cutting Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Cutting Finger Roll Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Cutting Layup Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Reverse Dunk Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Reverse Layup Shot'), \ 'SHOT_MADE_FLAG'].sum() attempt_off_dribble_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Pullup Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Pullup Jump shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Pull-Up Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Floating Jump shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_off_dribble_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Pullup Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Pullup Jump shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Running Pull-Up Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Floating Jump shot'), \ 'SHOT_MADE_FLAG'].sum() attempt_jumper_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Fadeaway Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Fadeaway Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Jump Bank Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Jump shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Step Back Bank Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Step Back Jump shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_jumper_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Fadeaway Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Fadeaway Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Jump Bank Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Driving Jump shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Step Back Bank Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Step Back Jump shot'), \ 'SHOT_MADE_FLAG'].sum() attempt_post_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Turnaround Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Fadeaway Bank Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Fadeaway shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Hook Bank Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Bank Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Hook Shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_post_2 = shots.loc[(shots['SHOT_GRP']== '2PT Field Goal_Turnaround Bank shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Fadeaway Bank Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Fadeaway shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Hook Bank Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Jump Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Turnaround Bank Hook Shot')| \ (shots['SHOT_GRP']== '2PT Field Goal_Hook Shot'), \ 'SHOT_MADE_FLAG'].sum() attempt_off_dribble_3 = shots.loc[(shots['SHOT_GRP']== '3PT Field Goal_Running Pull-Up Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Pullup Jump shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Running Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Pullup Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Bank Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Floating Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Floating Bank Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Floating Jump shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_off_dribble_3 = shots.loc[(shots['SHOT_GRP']== '3PT Field Goal_Running Pull-Up Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Pullup Jump shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Running Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Pullup Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Bank Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Floating Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Driving Floating Bank Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Floating Jump shot'), \ 'SHOT_MADE_FLAG'].sum() attempt_jumper_3 = shots.loc[(shots['SHOT_GRP']== '3PT Field Goal_Step Back Jump shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Jump Bank Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Fadeaway Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Fadeaway Bank shot') |\ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Bank Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Fadeaway Bank Jump Shot')|\ (shots['SHOT_GRP']== '3PT Field Goal_Step Back Bank Jump Shot')|\ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Fadeaway shot'), \ 'SHOT_ATTEMPTED_FLAG'].sum() made_jumper_3 = shots.loc[(shots['SHOT_GRP']== '3PT Field Goal_Step Back Jump shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Jump Bank Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Fadeaway Jump Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Fadeaway Bank shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Bank Hook Shot')| \ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Fadeaway Bank Jump Shot')|\ (shots['SHOT_GRP']== '3PT Field Goal_Step Back Bank Jump Shot')|\ (shots['SHOT_GRP']== '3PT Field Goal_Turnaround Fadeaway shot'), \ 'SHOT_MADE_FLAG'].sum() temp_dict = { 'player_id': str(id), 'total_attempt': float(total_attempt), 'total_made': float(total_made), 'attempt_2': float(attempt_2), 'made_2': float(made_2), 'attempt_3': float(attempt_3), 'made_3': float(made_3), 'attempt_drive_2': float(attempt_drive_2), 'made_drive_2': float(made_drive_2), 'attempt_at_rim_2': float(attempt_at_rim_2), 'made_at_rim_2': float(made_at_rim_2), 'attempt_cut_run_2': float(attempt_cut_run_2), 'made_cut_run_2': float(made_cut_run_2), 'attempt_off_dribble_2': float(attempt_off_dribble_2), 'made_off_dribble_2': float(made_off_dribble_2), 'attempt_jumper_2': float(attempt_jumper_2), 'made_jumper_2': float(made_jumper_2), 'attempt_off_dribble_3': float(attempt_off_dribble_3), 'made_off_dribble_3': float(made_off_dribble_3), 'attempt_jumper_3': float(attempt_jumper_3), 'made_jumper_3': float(made_jumper_3), 'attempt_post_2': float(attempt_post_2), 'made_post_2': float(made_post_2) } lst_of_dicts.append(temp_dict) else: temp_dict_empty = { 'player_id': str(id), 'total_attempt': float(0), 'total_made': float(0), 'attempt_2': float(0), 'made_2': float(0), 'attempt_3': float(0), 'made_3': float(0), 'attempt_drive_2': float(0), 'made_drive_2': float(0), 'attempt_at_rim_2': float(0), 'made_at_rim_2': float(0), 'attempt_cut_run_2': float(0), 'made_cut_run_2': float(0), 'attempt_off_dribble_2': float(0), 'made_off_dribble_2': float(0), 'attempt_jumper_2': float(0), 'made_jumper_2': float(0), 'attempt_off_dribble_3': float(0), 'made_off_dribble_3': float(0), 'attempt_jumper_3': float(0), 'made_jumper_3': float(0), 'attempt_post_2': float(0), 'made_post_2': float(0) } lst_of_dicts.append(temp_dict_empty) # time.sleep(1) player_shot_df = pd.DataFrame(lst_of_dicts) player_shot_df.set_index('player_id', inplace=True, drop=True) return player_shot_df
def test(): pid = get_player('Kevin', 'Durant') assert shotchart.ShotChart(pid)
if int(TO_YEAR) > 1996: #find pid and name of each player and format them how we need name = str(i.DISPLAY_FIRST_LAST).split() name = ' '.join(name[1:3]) #need to format pid in this way to store in dictionary pid = str(i["PERSON_ID"]).split()[1] pid_dict[pid] = name #loop through all seasons that have data for idx2, j in enumerate(seasons): first_half = j[:4] #finally add shot data to list if int(first_half) >= int(FROM_YEAR): #get shot dataframe from nba.com shots = shotchart.ShotChart(pid, season=j).shot_chart() #dont do anything if dataframe is empty if shots.empty: pass #calculate percentage by zone, add percentages to dataframe #and write to csv file else: percentage_dict = calc_all_zone_percentage(shots=shots, season=j) shots = add_zone_percentages(shots, percentage_dict) write_to_csv(shots=shots, pid=pid, year=j) #pickle player id dictionary for future use pickle.dump(pid_dict, open('pid_dict.pickle', 'wb+'))
def test(): pid = get_player('Lebron', 'James') #find errror handler for invalid name assert shotchart.ShotChart( pid) #find any case-sensitive/player names including " - " , " ' "
def getData(name, type, season=''): season = str(season) if type.lower() == 'shotlog': id = getPlayerId(name) year = season + '-' + str(int(season) + 1)[-2:] shot_data = shotchart.ShotChart(id, season=year).json data = shot_data['resultSets'][0]['rowSet'] indices = range(0, len(data)) colnames = shot_data['resultSets'][0]['headers'] df = pd.DataFrame(data, index=indices, columns=colnames) df = df.sort_values( ['GAME_DATE', 'PERIOD', 'MINUTES_REMAINING', 'SECONDS_REMAINING'], ascending=[1, 1, 0, 0]) return df elif type.lower() == 'pergame': id = getPlayerId(name) df = pd.DataFrame( player.PlayerCareer(id, 'PerGame').regular_season_totals()) df.columns = list(map(lambda x: x.upper(), df)) if season: year = season + '-' + str(int(season) + 1)[-2:] df = df.loc[df['SEASON_ID'] == year] return df elif type.lower() == 'gamelog': id = getPlayerId(name) try: season + '-' + str(int(season) + 1)[-2:] except: return 'Season is required. Please fill.' year = season + '-' + str(int(season) + 1)[-2:] df = player.PlayerGameLogs(id, '00', year, 'Regular Season').info() df['GAME_DATE'] = pd.to_datetime(pd.to_datetime( df['GAME_DATE'], infer_datetime_format=True), format='%Y%m%d') return df.sort_values(['GAME_DATE']) elif type.lower() == 'defense': id = getPlayerId(name) year = season + '-' + str(int(season) + 1)[-2:] player.PlayerDefenseTracking(id, 0) json = player.PlayerDefenseTracking(getPlayerId(name), 0, measure_type='Base', per_mode='PerGame', season=year).json data = json['resultSets'][0]['rowSet'] indices = [x[3] for x in data] # Set DEFENSE_CATEGORY as index colnames = json['resultSets'][0]['headers'] df = pd.DataFrame(data, index=indices, columns=colnames) df = df.drop(['CLOSE_DEF_PERSON_ID', 'DEFENSE_CATEGORY'], axis=1) return 'PlayerName: ' + name.upper(), 'Season: ' + year, df elif type.lower() == 'shotmap': id = getPlayerId(name) year = season + '-' + str(int(season) + 1)[-2:] shot_data = shotchart.ShotChart(id, season=year).json data = shot_data['resultSets'][0]['rowSet'] indices = range(0, len(data)) colnames = shot_data['resultSets'][0]['headers'] df = pd.DataFrame(data, index=indices, columns=colnames) df = df.sort_values( ['GAME_DATE', 'PERIOD', 'MINUTES_REMAINING', 'SECONDS_REMAINING'], ascending=[1, 1, 0, 0]) df_size = df.groupby(['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']).size() df_made = df.loc[df['SHOT_MADE_FLAG'] == 1].groupby( ['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']).size() df_pct = df_made / df_size df_pct = df_pct.fillna(0) df_size = df_size.to_frame('SHOTS_ATT').reset_index() df_made = df_made.to_frame('SHOTS_MADE').reset_index() df_pct = df_pct.to_frame('SHOT_PCT').reset_index() df = df_made.merge(df_size, how='right', on=['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']).fillna(int(0)).merge( df_pct, on=['SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE']) df['SHOTS_MADE'] = df['SHOTS_MADE'].astype(int) return df