def custom_boxscore(roster_id): game_logs = team.TeamGameLogs(roster_id) df_game_logs = game_logs.info() df_game_logs['GAME_DATE'] = pd.to_datetime(df_game_logs['GAME_DATE']) df_game_logs['DAYS_REST'] = df_game_logs['GAME_DATE'] - df_game_logs[ 'GAME_DATE'].shift(-1) df_game_logs['DAYS_REST'] = df_game_logs['DAYS_REST'].astype( 'timedelta64[D]') df_all = pd.DataFrame() ##blank dataframe dates = df_game_logs['GAME_DATE'] for date in dates: game_info = team.TeamPassTracking(roster_id, date_from=date, date_to=date).passes_made() game_info[ 'GAME_DATE'] = date ## We need to append the date to this so we can join back temp_df = game_info.groupby(['GAME_DATE']).sum() temp_df.reset_index(level=0, inplace=True) ##now to get the shot info. For the most part, we're just reusing code we've already written open_info = team.TeamShotTracking( roster_id, date_from=date, date_to=date).closest_defender_shooting() open_info['OPEN'] = open_info['CLOSE_DEF_DIST_RANGE'].map( lambda x: True if 'Open' in x else False) temp_df['OPEN_SHOTS'] = open_info.loc[open_info['OPEN'] == True, 'FGA'].sum() temp_df['OPEN_EFG'] = ( open_info.loc[open_info['OPEN'] == True, 'FGM'].sum() + (.5 * open_info.loc[open_info['OPEN'] == True, 'FG3M'].sum())) / ( open_info.loc[open_info['OPEN'] == True, 'FGA'].sum()) temp_df['COVERED_EFG'] = ( open_info.loc[open_info['OPEN'] == False, 'FGM'].sum() + (.5 * open_info.loc[open_info['OPEN'] == False, 'FG3M'].sum())) / ( open_info.loc[open_info['OPEN'] == False, 'FGA'].sum()) ##append this to our bigger dataframe df_all = df_all.append(temp_df) df_boxscore = pd.merge(df_game_logs, df_all[[ 'PASS', 'FG2M', 'FG2_PCT', 'OPEN_SHOTS', 'OPEN_EFG', 'COVERED_EFG' ]], how='left', left_on=df_game_logs['GAME_DATE'], right_on=df_all['GAME_DATE']) df_boxscore['PASS_AST'] = df_boxscore['PASS'] / df_boxscore['AST'] df_boxscore['RESULT'] = df_boxscore['WL'].map(lambda x: 1 if 'W' in x else 0) return df_boxscore
def test(): team_id = TEAMS['ATL']['id'] player_id = get_player('Lebron', 'James') assert team.TeamList() assert team.TeamSummary(team_id) team_details = team.TeamDetails(team_id) assert team_details assert team_details.background() assert team_details.history() assert team.TeamCommonRoster(team_id) assert team.TeamGeneralSplits(team_id) assert team.TeamOpponentSplits(team_id) assert team.TeamLastNGamesSplits(team_id) assert team.TeamInGameSplits(team_id) assert team.TeamClutchSplits(team_id) assert team.TeamShootingSplits(team_id) assert team.TeamPerformanceSplits(team_id) assert team.TeamLineups(team_id) assert team.TeamPlayers(team_id) assert team.TeamPlayerOnOffDetail(team_id) assert team.TeamPlayerOnOffSummary(team_id) assert team.TeamGameLogs(team_id) assert team.TeamShotTracking(team_id) assert team.TeamReboundTracking(team_id) assert team.TeamPassTracking(team_id) assert team.TeamVsPlayer(team_id, player_id)
def test(): if os.path.exists(TEAMS_FILE): teams = pd.DataFrame.from_csv(TEAMS_FILE) else: teams = team.TeamList().info().dropna() teams.to_csv(TEAMS_FILE) all = None for index, row in teams.iterrows(): data = team.TeamPassTracking(team_id=row['TEAM_ID']) passes_made = data.passes_made().drop([u'PASS_TYPE'], 1) passes_made.rename(columns={'PASS_FROM': 'PLAYER_NAME'}, inplace=True) passes_recieved = data.passes_recieved().drop([u'PASS_TYPE'], 1) passes_recieved.rename(columns={'PASS_TO': 'PLAYER_NAME'}, inplace=True) overall = passes_made.merge( passes_recieved, left_on=PRIMARY_KEYS, right_on=PRIMARY_KEYS, how='outer', suffixes=['_ON_MADE', '_ON_RECEIVED' ]).dropna().drop(['TEAM_ID', 'PASS_TEAMMATE_PLAYER_ID'], 1) if all is not None: all = pd.concat([all, overall]) else: all = overall print overall all.to_csv('data/passdash.csv', index=False)
def testAll(self): assert team.TeamList() assert team.TeamSummary(self.teamId) team_details = team.TeamDetails(self.teamId) assert team_details # assert team_details.background() # assert team_details.history() assert team.TeamCommonRoster(self.teamId) assert team.TeamGeneralSplits(self.teamId) assert team.TeamOpponentSplits(self.teamId) assert team.TeamLastNGamesSplits(self.teamId) assert team.TeamInGameSplits(self.teamId) assert team.TeamClutchSplits(self.teamId) assert team.TeamShootingSplits(self.teamId) assert team.TeamPerformanceSplits(self.teamId) assert team.TeamLineups(self.teamId) assert team.TeamPlayers(self.teamId) assert team.TeamPlayerOnOffDetail(self.teamId) assert team.TeamPlayerOnOffSummary(self.teamId) assert team.TeamGameLogs(self.teamId) assert team.TeamShotTracking(self.teamId) assert team.TeamReboundTracking(self.teamId) assert team.TeamPassTracking(self.teamId) assert team.TeamVsPlayer(self.teamId, self.playerId)
def nba_main(): raptors = team.TeamPassTracking(raptors_id) rp = raptors.passes_made().head() print(rp)
def nba_passes(): raptors_last_game = team.TeamPassTracking(raptors_id,last_n_games=2) raptors_last_game.passes_made().head()
import os import pandas as pd TEAMS_FILE = 'teams.csv' PRIMARY_KEYS = [ u'TEAM_ID', u'TEAM_NAME', u'PASS_TEAMMATE_PLAYER_ID', u'PLAYER_NAME' ] if __name__ == '__main__': if os.path.exists(TEAMS_FILE): teams = pd.DataFrame.from_csv(TEAMS_FILE) else: teams = team.TeamList().info().dropna() teams.to_csv(TEAMS_FILE) all = None for index, row in teams.iterrows(): data = team.TeamPassTracking(team_id=row['TEAM_ID']) passes_made = data.passes_made().drop([u'PASS_TYPE'], 1) passes_made.rename(columns={'PASS_FROM': 'PLAYER_NAME'}, inplace=True) passes_recieved = data.passes_recieved().drop([u'PASS_TYPE'], 1) passes_recieved.rename(columns={'PASS_TO': 'PLAYER_NAME'}, inplace=True) overall = passes_made.merge( passes_recieved, left_on=PRIMARY_KEYS, right_on=PRIMARY_KEYS, how='outer', suffixes=['_ON_MADE', '_ON_RECEIVED' ]).dropna().drop(['TEAM_ID', 'PASS_TEAMMATE_PLAYER_ID'], 1)