def test_2017_season_schedule_csv(self): season_schedule(season_end_year=2001, output_type=OutputType.CSV, output_file_path=self.output_2001_file_path) with open(self.output_2001_file_path, "r") as output_file, \ open(self.expected_output_2001_file_path, "r") as expected_output_file: self.assertEqual(output_file.readlines(), expected_output_file.readlines())
def test_writing_2017_season_schedule_json_file(self): season_schedule(season_end_year=2001, output_type=OutputType.JSON, output_file_path=self.output_2001_file_path) with open(self.output_2001_file_path, "r") as output_file, \ open(self.expected_output_2001_file_path, "r") as expected_output_file: self.assertEqual( json.load(output_file), json.load(expected_output_file), )
def get_schedule_df(end_yr): if 'schedules' not in listdir('.'): mkdir('schedules') fname = '{}-{}_schedule.csv'.format(end_yr - 1, str(end_yr)[-2:]) path = f'schedules/{fname}' if fname not in listdir('schedules'): client.season_schedule(season_end_year=end_yr, output_type=OutputType.CSV, output_file_path=path) return pd.read_csv(path)
def upload_data(): client.season_schedule(season_end_year=2014, output_type=OutputType.CSV, output_file_path = './data/game_results_2013_2014.csv') client.season_schedule(season_end_year=2015, output_type=OutputType.CSV, output_file_path = './data/game_results_2014_2015.csv') client.season_schedule(season_end_year=2016, output_type=OutputType.CSV, output_file_path = './data/game_results_2015_2016.csv') client.season_schedule(season_end_year=2017, output_type=OutputType.CSV, output_file_path = './data/game_results_2016_2017.csv') client.season_schedule(season_end_year=2018, output_type=OutputType.CSV, output_file_path = './data/game_results_2017_2018.csv') client.players_season_totals(season_end_year=2013, output_type=OutputType.JSON, output_file_path = './player_stats_2012_2013.json') client.players_season_totals(season_end_year=2014, output_type=OutputType.JSON, output_file_path = './player_stats_2013_2014.json') client.players_season_totals(season_end_year=2015, output_type=OutputType.JSON, output_file_path = './player_stats_2014_2015.json') client.players_season_totals(season_end_year=2016, output_type=OutputType.JSON, output_file_path = './player_stats_2015_2016.json') client.players_season_totals(season_end_year=2017, output_type=OutputType.JSON, output_file_path = './player_stats_2016_2017.json') client.players_season_totals(season_end_year=2018, output_type=OutputType.JSON, output_file_path = './player_stats_2017_2018.json')
def regularSeasonTeam(teamName): seasonSchedule = client.season_schedule(season_end_year=2019) teamRecord = [] wins = 0 losses = 0 for x in range(len(seasonSchedule)): if(seasonSchedule[x]['away_team_score'] == None): break else: if(str(seasonSchedule[x]['away_team']).replace("Team.", "").replace("_", "") == teamName or teamName in str(seasonSchedule[x]['away_team']).replace("Team.", "").replace("_", "")): if(seasonSchedule[x]['away_team_score'] > seasonSchedule[x]['home_team_score']): wins += 1 if(seasonSchedule[x]['away_team_score'] < seasonSchedule[x]['home_team_score']): losses += 1 if(str(seasonSchedule[x]['home_team']).replace("Team.", "").replace("_", "") == teamName or teamName in str(seasonSchedule[x]['home_team']).replace("Team.", "").replace("_", "")): if(seasonSchedule[x]['home_team_score'] > seasonSchedule[x]['away_team_score']): wins += 1 if(seasonSchedule[x]['home_team_score'] < seasonSchedule[x]['away_team_score']): losses += 1 teamRecord.append(wins) teamRecord.append(losses) return teamRecord
def add_points(season_end_year=2018): start_time = time.time() #for timing how this takes path = 'scraped_data_combined/' + str(season_end_year - 1) + 'to' + \ str(season_end_year) + 'combined.csv' df = pd.read_csv(path) df.insert(df.shape[1] - 1, 'points', 0) games = client.season_schedule(season_end_year=season_end_year) est = pytz.timezone("US/Eastern") for game in games: date = game['start_time'].astimezone(est).strftime('%Y-%m-%d') home_row_num = df.loc[(df['team'] == game['home_team'].value) & \ (df['date'] == date)].index[0] away_row_num = df.loc[(df['team'] == game['away_team'].value) & \ (df['date'] == date)].index[0] df['points'][home_row_num] = game['home_team_score'] df['points'][away_row_num] = game['away_team_score'] print(date + ': ' + game['away_team'].value + ' at ' + game['home_team'].value) df.to_csv(path, index=False, encoding='utf-8-sig') end_time = time.time() #for timing how this takes print('Time required = %d s.' % round(end_time - start_time))
def test_2018_season_schedule_in_memory_json(self): result = season_schedule(season_end_year=2018, output_type=OutputType.JSON) with open(self.expected_output_2018_file_path, "r") as expected_output_file: self.assertEqual(json.loads(result), json.load(expected_output_file))
def initialize_data(): ppg = {} oppg = {} sched = client.season_schedule(season_end_year=2021) eastern = list(map(utc_to_local, sched)) #now_est = datetime.now(timezone(-timedelta(hours=5))) #todays_games = list(filter(lambda x: (x['start_time'].month == now_est.month) and (x['start_time'].day == now_est.day), eastern)) todays_games = list( filter( lambda x: (x['start_time'].month == 12) and (x['start_time'].day == 25), eastern)) homes = [] aways = [] for game in todays_games: homes.append(game['home_team'].name) aways.append(game['away_team'].name) todays_games = list(zip(homes, aways)) todays_games = list( map(lambda x: x[0].split('_')[-1] + " @ " + x[1].split('_')[-1], todays_games)) with open('../data/ppg_avgs.csv', 'r', newline='') as csvfile: rows = csvfile.readlines() for row in rows: spl = row.split(',') teamname = spl[0] ppg[teamname] = float(spl[1]) oppg[teamname] = float(spl[2]) return todays_games, ppg, oppg
def test_schedules_from_2001(self): now = datetime.now() current_year = now.year for year in range(2001, current_year + 1): season_schedule = client.season_schedule(season_end_year=year) self.assertIsNotNone(season_schedule)
def getDataLocal(val, d=curd, m=curm, y=cury): global finstr if (val == 0): fstr = str(d) + "_" + str(m) + "_" + str( y) + "_" + "player_box_scores.json" finstr = finstr + fstr client.player_box_scores( day=d, month=m, year=y, output_type=OutputType.JSON, output_file_path= "/Users/pranavkrishnakumar/repos/BeardBot/Data/player_box_scores/" + fstr) elif (val == 1): fstr = str(d) + "_" + str(m) + "_" + str( y) + "_" + "team_box_scores.json" finstr = finstr + fstr client.team_box_scores( day=d, month=m, year=y, output_type=OutputType.JSON, output_file_path= "/Users/pranavkrishnakumar/repos/BeardBot/Data/team_box_scores/" + fstr) elif (val == 2): fstr = str(y) + "_" + "season_schedule" + ".json" finstr = finstr + fstr client.season_schedule( season_end_year=y, output_type=OutputType.JSON, output_file_path= "/Users/pranavkrishnakumar/repos/BeardBot/Data/season_schedule/" + fstr) elif (val == 3): fstr = str(y) + "_" + "season_standings" + ".json" finstr = finstr + fstr client.standings( season_end_year=y, output_type=OutputType.JSON, output_file_path= "/Users/pranavkrishnakumar/repos/BeardBot/Data/season_standings/" + fstr) return val
def get_schedule_range(start_yr, end_yr): schedules = [] for year in range(start_yr + 1, end_yr + 1): try: schedule = client.season_schedule(season_end_year=year) schedules.append(schedule) except: pass return schedules
def pull(connection, start=None): #use season start if not specified if start is None: sched = client.season_schedule(2019) start = sched[0].get('start_time').date() current = date.today() #add all game logs from given start to date for gameday in daterange(start, current): update(connection, gameday)
def getData(val, d=curd, m=curm, y=cury): global finstr if (val == 0): return client.player_box_scores(day=d, month=m, year=y) elif (val == 1): return client.team_box_scores(day=d, month=m, year=y) elif (val == 2): return client.season_schedule(season_end_year=y) elif (val == 3): return client.standings(season_end_year=y)
def test_last_game_of_2018_season(self): result = season_schedule(season_end_year=2018) self.assertEqual( result[1311], { "away_team": Team.GOLDEN_STATE_WARRIORS, "away_team_score": 108, "home_team": Team.CLEVELAND_CAVALIERS, "home_team_score": 85, "start_time": datetime(2018, 6, 9, 1, 0, tzinfo=pytz.utc) })
def test_first_game_of_2018_season(self): result = season_schedule(season_end_year=2018) self.assertEqual( result[0], { "away_team": Team.BOSTON_CELTICS, "away_team_score": 99, "home_team": Team.CLEVELAND_CAVALIERS, "home_team_score": 102, "start_time": datetime(2017, 10, 18, 0, 1, tzinfo=pytz.utc), }, )
def get_rolling_team_stats(season): schedule = client.season_schedule(season_end_year=season) all_team_stats = {} games_played = 0 for game in schedule: games_played += 1 if games_played == utils.get_num_games(season): break return start_date, end_date
def get_reg_season_start_end_dates(season): schedule = client.season_schedule(season_end_year=season) start_date = utils.get_pacific_date(schedule[0][START_TIME]) end_date = None games_played = 0 for game in schedule: games_played += 1 end_date = utils.get_pacific_date(game[START_TIME]) if games_played == utils.get_num_games(season): break return start_date, end_date
def get_all_games(self): schedule = client.season_schedule(season_end_year=2019) games = [] today = datetime.now(timezone.utc) #one day offset yesterday = today.day - 1 today = today.replace(day=yesterday) for game in schedule: game_day = game['start_time'] if ((today - game_day).days >= 0): if (game['home_team'].value == self.team): games.append(game['start_time']) elif (game['away_team'].value == self.team): games.append(game['start_time']) return games
def process_play_by_play(*, year: int, output_dir: str) -> None: sched = client.season_schedule(season_end_year=year) failed_game_descriptions = [] for game in sched: home_team = game['home_team'].name start_time = _convert_time_from_utc(game['start_time']) start_time_str = start_time.strftime('%Y-%m-%d') game_description = f"[{start_time_str}]: Pulling data for home team {home_team}" print(game_description) try: play_by_play = client.play_by_play( home_team=game['home_team'], year=start_time.year, month=start_time.month, day=start_time.day, ) except Exception: print("FAILURE! Skipping game") failed_game_descriptions.append(game_description) continue for play in play_by_play: play['date'] = start_time_str play['period_type'] = play['period_type'].name play['away_team'] = play['away_team'].name play['home_team'] = play['home_team'].name output_filename = f"game_date={start_time_str}_hometeam={home_team}.tsv" pd.DataFrame.from_records(play_by_play).to_csv( os.path.join(output_dir, output_filename), header=True, index=False, sep='\t', ) # show failures if failed_game_descriptions: print(f"****{len(failed_game_descriptions)} games failed to parse****") for description in failed_game_descriptions: print(description)
def test_season_schedule_json(self): result = season_schedule(season_end_year=2018, output_type=OutputType.JSON) self.assertIsNotNone(result)
def test_2010_season_schedule(self): player_season_totals = client.season_schedule(season_end_year=2010) self.assertIsNotNone(player_season_totals)
def test_2018_season_schedule_length(self): result = season_schedule(season_end_year=2018) self.assertEqual(1312, len(result))
def main(): # Start by including the headers. examples = [[ 'outcome', 'team0_sn_fg', 'team0_sn_fga', 'team0_sn_fgp', 'team0_sn_3pfg', 'team0_sn_3pfga', 'team0_sn_fg_3pfgp', 'team0_sn_2pfg', 'team0_sn_2pfga', 'team0_sn_2pfgp', 'team0_sn_ft', 'team0_sn_fta', 'team0_sn_ftp', 'team0_sn_orb', 'team0_sn_drb', 'team0_sn_trb', 'team0_sn_ast', 'team0_sn_stl', 'team0_sn_blk', 'team0_sn_tov', 'team0_sn_pf', 'team0_sn_pts', 'team1_sn_fg', 'team1_sn_fga', 'team1_sn_fgp', 'team1_sn_3pfg', 'team1_sn_3pfga', 'team1_sn_fg_3pfgp', 'team1_sn_2pfg', 'team1_sn_2pfa', 'team1_sn_2pfgp', 'team1_sn_ft', 'team1_sn_fta', 'team1_sn_ftp', 'team1_sn_orb', 'team1_sn_drb', 'team1_sn_trb', 'team1_sn_ast', 'team1_sn_stl', 'team1_sn_blk', 'team1_sn_tov', 'team1_sn_pf', 'team1_sn_pts', 'team0_matchup_fg', 'team0_matchup_fga', 'team0_matchup_fgp', 'team0_matchup_3pfg', 'team0_matchup_3pfga', 'team0_matchup_fg_3pfgp', 'team0_matchup_ft', 'team0_matchup_fta', 'team0_matchup_ftp', 'team0_matchup_orb', 'team0_matchup_drb', 'team0_matchup_trb', 'team0_matchup_ast', 'team0_matchup_stl', 'team0_matchup_blk', 'team0_matchup_tov', 'team0_matchup_pf', 'team0_matchup_pts', 'team1_matchup_fg', 'team1_matchup_fga', 'team1_matchup_fgp', 'team1_matchup_3pfg', 'team1_matchup_3pfga', 'team1_matchup_fg_3pfgp', 'team1_matchup_ft', 'team1_matchup_fta', 'team1_matchup_ftp', 'team1_matchup_orb', 'team1_matchup_drb', 'team1_matchup_trb', 'team1_matchup_ast', 'team1_matchup_stl', 'team1_matchup_blk', 'team1_matchup_tov', 'team1_matchup_pf', 'team1_matchup_pts' ]] for season in range( 1998, 2020): # The 1997-2000 season until the 2017-2018 season. season_schedule = client.season_schedule(season_end_year=season) url = 'https://www.basketball-reference.com/playoffs/NBA_' + str( season) + '.html' playoff_outcomes = requests.get(url) data = list( html.fromstring(playoff_outcomes.content).xpath( '//table[@id="all_playoffs"]' '/tbody' '/tr[not(contains(@class, "thead") or contains(@class, "toggleable"))]' '/td/a/text()')) data = [d for d in data if d != 'Series Stats'] it = iter(data) data = list(zip(it, it)) teams_appearing_in_playoffs = list( set([series[0] for series in data] + [series[1] for series in data])) print(teams_appearing_in_playoffs) # Get the winning team's season average box scores for series in data: if series[0] not in shortnames or series[ 1] not in shortnames or series[ 0] == 'Charlotte Hornets' or series[ 1] == 'Charlotte Hornets': continue print("Parsing series: " + str(series)) team1_averages = team_average_box_scores(shortnames[series[0]], season) team2_averages = team_average_box_scores(shortnames[series[1]], season) team1_season_matchup_averages = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] team2_season_matchup_averages = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] games_played = 0 for game in season_schedule: if game['home_team'].value.lower() == series[0].lower( ) and game['away_team'].value.lower() == series[1].lower(): date = game['start_time'] url_date = format_date_for_url(date.date().year, date.date().month, date.date().day) url = 'https://www.basketball-reference.com/boxscores/' + url_date[ 0] + url_date[1] + url_date[2] + '0' + shortnames[ series[0]] + '.html' match = requests.get(url) game_soup = BeautifulSoup(match.content, "html.parser") if match.status_code == 404 or series[1] + " at " + series[ 0] not in game_soup.find( "div", id="content").find("h1").text: date -= timedelta(1) url_date = format_date_for_url(date.date().year, date.date().month, date.date().day) url = 'https://www.basketball-reference.com/boxscores/' + url_date[ 0] + url_date[1] + url_date[2] + '0' + shortnames[ series[0]] + '.html' match = requests.get(url) game_soup = BeautifulSoup(match.content, "html.parser") if match.status_code == 404 or series[ 1] + " at " + series[0] not in game_soup.find( "div", id="content").find("h1").text: continue game_soup = BeautifulSoup(match.content, "html.parser") home_team_score_elements = game_soup.find("table", id="box-" + shortnames[series[0]] + "-game-basic")\ .find("tfoot").find_all("td")[1:-1] home_team_scores = [ td.text for td in home_team_score_elements ] away_team_score_elements = game_soup.find("table", id="box-" + shortnames[series[1]] + "-game-basic")\ .find("tfoot").find_all("td")[1:-1] away_team_scores = [ td.text for td in away_team_score_elements ] for i in range(18): team1_season_matchup_averages[i] += float( home_team_scores[i]) team2_season_matchup_averages[i] += float( away_team_scores[i]) games_played += 1 print(home_team_scores) print(away_team_scores) elif game['home_team'].value.lower() == series[1].lower( ) and game['away_team'].value.lower() == series[0].lower(): date = game['start_time'] url_date = format_date_for_url(date.date().year, date.date().month, date.date().day) url = 'https://www.basketball-reference.com/boxscores/' + str( date.date().year) + url_date[0] + url_date[ 1] + url_date[2] + '0' + shortnames[ series[1]] + '.html' match = requests.get(url) game_soup = BeautifulSoup(match.content, "html.parser") if match.status_code == 404 or series[0] + " at " + series[ 1] not in game_soup.find( "div", id="content").find("h1").text: date -= timedelta(1) url_date = format_date_for_url(date.date().year, date.date().month, date.date().day) url = 'https://www.basketball-reference.com/boxscores/' + url_date[ 0] + url_date[1] + url_date[2] + '0' + shortnames[ series[1]] + '.html' match = requests.get(url) game_soup = BeautifulSoup(match.content, "html.parser") if match.status_code == 404 or series[ 0] + " at " + series[1] not in game_soup.find( "div", id="content").find("h1").text: continue game_soup = BeautifulSoup(match.content, "html.parser") home_team_score_elements = game_soup.find("table", id="box-" + shortnames[series[1]] + "-game-basic") \ .find("tfoot").find_all("td")[1:-1] home_team_scores = [ td.text for td in home_team_score_elements ] away_team_score_elements = game_soup.find("table", id="box-" + shortnames[series[0]] + "-game-basic") \ .find("tfoot").find_all("td")[1:-1] away_team_scores = [ td.text for td in away_team_score_elements ] for i in range(18): team1_season_matchup_averages[i] += float( away_team_scores[i]) team2_season_matchup_averages[i] += float( home_team_scores[i]) games_played += 1 else: continue # Compute averages team1_season_matchup_averages = [ stat / games_played for stat in team1_season_matchup_averages ] team2_season_matchup_averages = [ stat / games_played for stat in team2_season_matchup_averages ] data_point1 = [ 0 ] + team1_averages + team2_averages + team1_season_matchup_averages + team2_season_matchup_averages data_point2 = [ 1 ] + team2_averages + team1_averages + team2_season_matchup_averages + team1_season_matchup_averages examples.append(data_point1) examples.append(data_point2) # Store examples in CSV. with open("training_examples_1997-2019.csv", "w", newline="") as f: writer = csv.writer(f) writer.writerows(examples)
def test_2017_season_schedule_csv_append(self): season_schedule(season_end_year=2001, output_type=OutputType.CSV, output_file_path="./foo.csv", output_write_option=OutputWriteOption.WRITE)
def test_2015_season_schedule(self): schedule = client.season_schedule(season_end_year=2015) self.assertIsNotNone(schedule)
def test_season_schedule(self): result = season_schedule(season_end_year=2018) self.assertIsNotNone(result)
def get_averages(season_end_year=2018): start_time = time.time() #for timing how this takes df = pd.read_csv('season_combined.csv') df = df.sort_values(by=['season', 'team', 'date']) averages_file = open('averages/averages' + str(season_end_year - 1) + 'to' + \ str(season_end_year) + '.csv', 'w') str_to_write = 'season,date,home_team' for i in range(3, len(df.columns) - 1): str_to_write += ',' + df.columns[i] + '_home' for i in range(3, len(df.columns) - 1): str_to_write += ',' + df.columns[i] + '_away' str_to_write += ',' + df.columns[-1] str_to_write += ',home_won\n' averages_file.write(str_to_write) games = client.season_schedule(season_end_year=season_end_year) season = str(season_end_year - 1) + '-' + str(season_end_year) est = pytz.timezone("US/Eastern") for game in games: #can't calculate average from last 6 games if 6 games haven't been played yet date = game['start_time'].astimezone(est).strftime('%Y-%m-%d') home_prev_games = df.loc[(df['season'] == season) & \ (df['team'] == game['home_team'].value) & \ (df['date'] < date)] away_prev_games = df.loc[(df['season'] == season) & \ (df['team'] == game['away_team'].value) & \ (df['date'] < date)] if home_prev_games.shape[0] < 6 or away_prev_games.shape[0] < 6: continue #get averages from last 6 games and winner home_averages = home_prev_games.iloc[-6:].mean() away_averages = away_prev_games.iloc[-6:].mean() curr_game = df.loc[(df['team'] == game['home_team'].value) & \ (df['date'] == date)] home_won = 0 if game['home_team_score'] > game['away_team_score']: home_won = 1 str_to_write = season + ',' + date + ',' + game['home_team'].value for i in range(len(home_averages) - 1): str_to_write += ',' + str(home_averages[i]) for i in range(len(away_averages) - 1): str_to_write += ',' + str(away_averages[i]) str_to_write += ',' + str(curr_game['during_playoffs'].values[0]) str_to_write += ',' + str(home_won) + '\n' averages_file.write(str_to_write) print(date + ': ' + game['away_team'].value + ' at ' + game['home_team'].value) averages_file.close() end_time = time.time() #for timing how this takes print('Time required = %d s.' % round(end_time - start_time))
def get_rolling_player_season_stats(players, season): schedule = client.season_schedule(season_end_year=season) start_date, end_date = get_reg_season_start_end_dates(season) HOME_GAME = 1 AWAY_GAME = 0 #TODO: move to own file class rolling_player_stats(): def __init__(self): self.num_games = 0 self.rolling_avg_pts = [-1.] self.game_pts = [] self.home_not_away = [] self.opp_allowed_rating = [] def update_stats(self, points): self.game_pts.append(points) new_avg_pts = \ (self.rolling_avg_pts[-1] * self.num_games + points) / (self.num_games + 1) self.rolling_avg_pts.append(new_avg_pts) self.num_games += 1 #can be used for both individual and league average stats class rolling_team_stats(): def __init__(self): self.num_games = 0 self.avg_points_allowed = 0. def update_stats(self, points): self.avg_points_allowed = ( (self.avg_points_allowed * self.num_games) + points) / (self.num_games + 1) self.num_games += 1 player_stats_dict = {player: rolling_player_stats() for player in players} team_stats_dict = {} avg_team_stats = rolling_team_stats() game_number = 0 for ts in pd.date_range(start=start_date, end=end_date, freq='D'): date = ts.date() print("Processing date: {}".format(date)) #get all games for this date box_scores = client.player_box_scores(day=date.day, month=date.month, year=date.year) for player_box_score in box_scores: player = player_box_score[PLAYER_ID] if player not in player_stats_dict: continue #TODO: put calculations in rolling_player_stats player_stats = player_stats_dict[player] points = get_total_points(player_box_score) player_stats.home_not_away.append( HOME_GAME if player_box_score[LOCATION] == data.Location.HOME else AWAY_GAME) opposing_team = player_box_score[OPPONENT] if opposing_team in team_stats_dict: player_stats.opp_allowed_rating.append( team_stats_dict[opposing_team].avg_points_allowed / avg_team_stats.avg_points_allowed) else: player_stats.opp_allowed_rating.append(-1.) player_stats.update_stats(points) #update team stats while utils.get_pacific_date( schedule[game_number][START_TIME]) <= date: game = schedule[game_number] away_team = game[AWAY_TEAM] home_team = game[HOME_TEAM] if away_team not in team_stats_dict: team_stats_dict[away_team] = rolling_team_stats() if home_team not in team_stats_dict: team_stats_dict[home_team] = rolling_team_stats() team_stats_dict[away_team].update_stats(game[HOME_TEAM_SCORE]) team_stats_dict[home_team].update_stats(game[AWAY_TEAM_SCORE]) #update league avg team stats avg_team_stats.update_stats(game[HOME_TEAM_SCORE]) avg_team_stats.update_stats(game[AWAY_TEAM_SCORE]) game_number += 1 #convert to dataframe #Don't consider stats prior to this cutoff ROLLING_AVG_CUTOFF = 15 points_data = [] rolling_avg_pts_data = [] opp_allowed_rating_data = [] home_not_away_data = [] player_id_data = [] for player_id, player_stats in player_stats_dict.items(): num_games = player_stats.num_games - ROLLING_AVG_CUTOFF if num_games < MIN_GAMES or player_stats.rolling_avg_pts[-1] < MIN_PPG: continue player_id_data += (num_games * [player_id]) points_data += (player_stats.game_pts[ROLLING_AVG_CUTOFF:]) opp_allowed_rating_data += ( player_stats.opp_allowed_rating[ROLLING_AVG_CUTOFF:]) home_not_away_data += (player_stats.home_not_away[ROLLING_AVG_CUTOFF:]) #don't include last/final ppg rolling_avg_pts_data += ( player_stats.rolling_avg_pts[ROLLING_AVG_CUTOFF:-1]) return pd.DataFrame({ constants.PLAYER_ID_COL: player_id_data, constants.PPG_COL: rolling_avg_pts_data, constants.PTS_COL: points_data, constants.OPP_ALLOWED_RATING_COL: opp_allowed_rating_data, constants.HOME_NOT_AWAY_COL: home_not_away_data })
def main(): print("\n#######################################################################") print("#######################################################################") print("######################### NBA Report exporter #########################") print("#######################################################################") print("#######################################################################\n") while (True): print( "1. Players box scores by a date\ \n2. Players season statistics for a season\ \n3. Players advanced season statistics for a season\ \n4. All Team box scores by a date\ \n5. Schedule for a season\ \n6. Exit" ) reportObject = input("\nPlease select a option: ") # Players box scores by a date if (reportObject == "1"): inputDate = input("\nEnter a date (use this format 1-1-2018): ") fileName = "all-player-box-report-" + inputDate + ".csv" dateList = inputDate.split("-") print("Exporting report please wait..........") # Call Export function client.player_box_scores( day=dateList[0], month=dateList[1], year=dateList[2], output_type=OutputType.CSV, output_file_path="exported_files/" + fileName ) print("Report exported at: " + abspath("exported_files/" + fileName) + "!!\n\n") # Players season statistics for a season elif (reportObject == "2"): endYear = input("\nEnter season end year: ") fileName = "all-player-season-report-" + endYear + ".csv" print("Exporting report please wait..........") # Call Export function client.players_season_totals( season_end_year=endYear, output_type=OutputType.CSV, output_file_path="exported_files/" + fileName ) print("Report exported at: " + abspath("exported_files/" + fileName) + "!!\n\n") # Players advanced season statistics for a season elif (reportObject == "3"): endYear = input("\nEnter season end year: ") fileName = "all-player-advanced-season-report-" + endYear + ".csv" print("Exporting report please wait..........") # Call Export function client.players_advanced_season_totals( season_end_year=endYear, output_type=OutputType.CSV, output_file_path="exported_files/" + fileName ) print("Report exported at: " + abspath("exported_files/" + fileName) + "!!\n\n") # All Team box scores by a date elif (reportObject == "4"): inputDate = input("\nEnter a date (use this format 1-1-2018): ") fileName = "all-team-report-" + inputDate + ".csv" dateList = inputDate.split("-") print("Exporting report please wait..........") # Call Export function client.team_box_scores( day=dateList[0], month=dateList[1], year=dateList[2], output_type=OutputType.CSV, output_file_path="exported_files/" + fileName ) print("Report exported at: " + abspath("exported_files/" + fileName) + "!!\n\n") # Schedule for a season elif (reportObject == "5"): endYear = input("\nEnter season end year: ") fileName = "season-schedule-" + endYear + ".csv" print("Exporting report please wait..........") # Call Export function client.season_schedule( season_end_year=endYear, output_type=OutputType.CSV, output_file_path="exported_files/" + fileName ) print("Report exported at: " + abspath("exported_files/" + fileName) + "!!\n\n") # Exit elif (reportObject == "6"): print("\n#######################################################################") print("################################# Bye #################################") print("#######################################################################\n") break # Error else: print("Invalid option!!\n\n")
def test_season_schedule_csv(self): season_schedule(season_end_year=2018, output_type=OutputType.CSV, output_file_path="./foo.csv")