Skip to content

Instantly share code, notes, and snippets.

@databyjp
Created February 16, 2022 14:58
Show Gist options
  • Save databyjp/120f706256e516f3e30aadf3d3c6ab43 to your computer and use it in GitHub Desktop.
Save databyjp/120f706256e516f3e30aadf3d3c6ab43 to your computer and use it in GitHub Desktop.
import logging
import pandas as pd
import plotly.express as px
import json
import os
import utils
logger = logging.getLogger(__name__)
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
sh = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
sh.setFormatter(formatter)
root_logger.addHandler(sh)
desired_width = 320
pd.set_option('display.max_columns', 20)
pd.set_option('display.width', desired_width)
json_dir = "dl_data/box_scores/json"
json_files = [i for i in os.listdir(json_dir) if i.endswith("json")]
def box_json_to_df(content, data="team"):
if data == "team":
sel_int = 1
elif data == "player":
sel_int = 0
else:
logger.warning("")
results = content["resultSets"][sel_int]
headers = results["headers"]
rows = results["rowSet"]
df = pd.DataFrame(rows)
df.columns = headers
return df
df_list = list()
for json_file in json_files:
json_path = os.path.join(json_dir, json_file)
with open(json_path, 'r') as f:
content = json.load(f)
tdf = box_json_to_df(content, data="team")
df_list.append(tdf)
df = pd.concat(df_list)
df = df[df["GAME_ID"].str[:5] == "00221"]
gldf = utils.load_gamelogs()
df = pd.merge(
df,
gldf[["Game_ID", "gamedate_dt"]],
left_on="GAME_ID",
right_on="Game_ID",
how="left",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment