Last active
February 26, 2020 02:57
-
-
Save databyjp/addeda938ce9545d8437da459766f7f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get all team totals for all dates | |
temp_df_list = list() | |
for game_date in game_dates: | |
logger.info(f'Getting data for games on {game_date}') | |
temp_boxes = client.team_box_scores(day=game_date.day, month=game_date.month, year=game_date.year) | |
temp_df = pd.DataFrame(temp_boxes) | |
temp_df = temp_df.assign(game_date=str(game_date)) | |
temp_df_list.append(temp_df) | |
# ========== SET UP BOX SCORE DATAFRAME ========== | |
box_df = pd.concat(temp_df_list) | |
box_df.reset_index(drop=True, inplace=True) | |
box_df = box_df.assign(home=True) | |
for i, df_row in box_df.iterrows(): | |
gm_date = df_row.game_date | |
tm_str = df_row.astype(str)['team'][5:] | |
sch_row = schedule_df[ | |
((schedule_df.home_team == tm_str) | (schedule_df.away_team == tm_str)) | |
& (schedule_df.game_date == pd.to_datetime(gm_date).date()) | |
] | |
if sch_row.iloc[0].away_team == tm_str: | |
logger.info(f'Setting {tm_str} game on {gm_date} as an away game.') | |
box_df.loc[i, 'home'] = False | |
box_df.to_csv('srcdata/' + str(season_end_yr) + '_nba_team_box_raw.csv') | |
# ========== PROCESS DATA BASED ON TRAVEL & REST DAYS ========== | |
box_df = box_df.assign(rest_days=10) | |
box_df = box_df.assign(travelled=False) | |
for temp_tm in box_df.team.unique(): | |
# temp_tm = Team.TORONTO_RAPTORS | |
team_box_df = box_df[(box_df.team == temp_tm)] | |
# team_box_df.reset_index(drop=True, inplace=True) | |
for i in range(len(team_box_df)): | |
tmp_ind = team_box_df.iloc[i].name | |
if i > 0: | |
df_row = team_box_df.iloc[i] | |
temp_td = pd.to_datetime(team_box_df.iloc[i]['game_date']) - pd.to_datetime(team_box_df.iloc[i-1]['game_date']) | |
box_df.loc[tmp_ind, 'rest_days'] = temp_td.days - 1 | |
if df_row.home == False: | |
box_df.loc[tmp_ind, 'travelled'] = True | |
elif team_box_df.iloc[i-1].home == False: | |
box_df.loc[tmp_ind, 'travelled'] = True | |
box_df = box_df.assign(rest_cats=box_df.rest_days.clip(upper=2).astype(str).replace('2', '2+')) | |
box_df = box_df.assign(win=box_df.outcome == Outcome.WIN) | |
box_df.team = box_df.team.astype(str).str[5:] | |
box_df.outcome = box_df.outcome.astype(str).str[8:] | |
box_df.to_csv('srcdata/' + str(season_end_yr) + '_nba_team_box_scores.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment