{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# # creating a function which takes in a game id and returns the dataframe of the game's play by play data\n", "\n", "\n", "# def get_game_pbp(game_id):\n", "# # Construct the URL for the play-by-play endpoint\n", "# url = f\"https://api.sportradar.us/nba/trial/v8/en/games/{game_id}/pbp.json?api_key={api_key}\"\n", "\n", "# # Make a GET request to the endpoint\n", "# game_data = requests.get(url)\n", "\n", "# # Check if the request was successful\n", "# if game_data.status_code == 200:\n", "# # Convert the response to JSON\n", "# data = game_data.json()\n", "\n", "# # Extract the relevant information\n", "# scheduled = data[\"scheduled\"] # This is when the game was scheduled\n", "# home_team_name = data[\"home\"][\"market\"] + \" \" + data[\"home\"][\"name\"]\n", "# away_team_name = data[\"away\"][\"market\"] + \" \" + data[\"away\"][\"name\"]\n", "# descriptions = []\n", "# clocks = []\n", "# event_types = []\n", "# home_points = []\n", "# away_points = []\n", "# quarters = []\n", "\n", "# for period in data[\"periods\"]:\n", "# for event in period[\"events\"]:\n", "# descriptions.append(\n", "# event.get(\"description\", None)\n", "# ) # This is what happened\n", "# clocks.append(event.get(\"clock\", None)) # This is when it happened\n", "# event_types.append(\n", "# event.get(\"event_type\", None)\n", "# ) # This is the type of event\n", "# home_points.append(\n", "# event.get(\"home_points\", None)\n", "# ) # This is the home team's points\n", "# away_points.append(\n", "# event.get(\"away_points\", None)\n", "# ) # This is the visitor team's points\n", "# quarters.append(period.get(\"number\", None)) # This is the quarter\n", "\n", "# # Create a DataFrame from the extracted information\n", "# df = pd.DataFrame(\n", "# {\n", "# \"date\": scheduled, # This is when the game was scheduled\n", "# \"home_team_name\": home_team_name,\n", "# \"away_team_name\": away_team_name,\n", "# \"description\": descriptions,\n", "# \"clock\": clocks,\n", "# \"event_type\": event_types,\n", "# \"home_points\": home_points,\n", "# \"away_points\": away_points,\n", "# \"quarter\": quarters,\n", "# }\n", "# )\n", "\n", "# # Return the DataFrame\n", "# return df\n", "\n", "# else:\n", "# # If the request was not successful, return None\n", "# return None" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "json_files = []\n", "\n", "# Walk through current directory\n", "for dirpath, dirnames, filenames in os.walk('.'):\n", " # Find JSON files in each directory\n", " for filename in filenames:\n", " if filename.endswith('.json'):\n", " # Append the path to the file\n", " json_files.append(os.path.join(dirpath, filename))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "import pandas as pd\n", "\n", "def get_game_pbp_from_file(file_path):\n", " # Load JSON data from the file\n", " with open(file_path, 'r') as file:\n", " data = json.load(file)\n", "\n", " # Extract the relevant information\n", " id = data[\"id\"] # This is the game's ID\n", " scheduled = data[\"scheduled\"] # This is when the game was scheduled\n", " home_team_name = data[\"home\"][\"market\"] + \" \" + data[\"home\"][\"name\"]\n", " away_team_name = data[\"away\"][\"market\"] + \" \" + data[\"away\"][\"name\"]\n", " descriptions = []\n", " clocks = []\n", " event_types = []\n", " home_points = []\n", " away_points = []\n", " quarters = []\n", "\n", " if \"periods\" not in data:\n", " print (f\"File {file_path} does not contain play-by-play data\")\n", " return None\n", " \n", " else:\n", "\n", " for period in data[\"periods\"]:\n", " for event in period[\"events\"]:\n", " descriptions.append(event.get(\"description\", None)) # This is what happened\n", " clocks.append(event.get(\"clock\", None)) # This is when it happened\n", " event_types.append(event.get(\"event_type\", None)) # This is the type of event\n", " home_points.append(event.get(\"home_points\", None)) # This is the home team's points\n", " away_points.append(event.get(\"away_points\", None)) # This is the visitor team's points\n", " quarters.append(period.get(\"number\", None)) # This is the quarter\n", "\n", " # Create a DataFrame from the extracted information\n", " df = pd.DataFrame({\n", " \"id\": id, # This is the game's ID\n", " \"date\": scheduled, # This is when the game was scheduled\n", " \"home_team_name\": home_team_name,\n", " \"away_team_name\": away_team_name,\n", " \"description\": descriptions,\n", " \"clock\": clocks,\n", " \"event_type\": event_types,\n", " \"home_points\": home_points,\n", " \"away_points\": away_points,\n", " \"quarter\": quarters,\n", " })\n", "\n", " # Return the DataFrame\n", " return df\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just testing the function with one file" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatehome_team_nameaway_team_namedescriptionclockevent_typehome_pointsaway_pointsquarter
06decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersLakers lineup change (Thomas Bryant, LeBron Ja...12:00lineupchange001
16decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersTrail Blazers lineup change (Damian Lillard, J...12:00lineupchange001
26decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersThomas Bryant vs. Jusuf Nurkic (Dennis Schrode...12:00opentip001
36decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersLeBron James makes two point cutting dunk (Den...11:47twopointmade021
46decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersDamian Lillard makes two point jump shot (Josh...11:31twopointmade221
.................................
4586decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersDennis Schroder makes regular free throw 1 of 200:30freethrowmade1121204
4596decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersDennis Schroder makes regular free throw 2 of 200:30freethrowmade1121214
4606decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersDamian Lillard misses three point pullup jump ...00:24threepointmiss1121214
4616decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersLeBron James defensive rebound00:21rebound1121214
4626decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersEnd of 4th Quarter.00:00endperiod1121214
\n", "

463 rows × 10 columns

\n", "
" ], "text/plain": [ " id date \\\n", "0 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "1 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "2 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "3 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "4 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", ".. ... ... \n", "458 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "459 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "460 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "461 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "462 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "\n", " home_team_name away_team_name \\\n", "0 Portland Trail Blazers Los Angeles Lakers \n", "1 Portland Trail Blazers Los Angeles Lakers \n", "2 Portland Trail Blazers Los Angeles Lakers \n", "3 Portland Trail Blazers Los Angeles Lakers \n", "4 Portland Trail Blazers Los Angeles Lakers \n", ".. ... ... \n", "458 Portland Trail Blazers Los Angeles Lakers \n", "459 Portland Trail Blazers Los Angeles Lakers \n", "460 Portland Trail Blazers Los Angeles Lakers \n", "461 Portland Trail Blazers Los Angeles Lakers \n", "462 Portland Trail Blazers Los Angeles Lakers \n", "\n", " description clock event_type \\\n", "0 Lakers lineup change (Thomas Bryant, LeBron Ja... 12:00 lineupchange \n", "1 Trail Blazers lineup change (Damian Lillard, J... 12:00 lineupchange \n", "2 Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode... 12:00 opentip \n", "3 LeBron James makes two point cutting dunk (Den... 11:47 twopointmade \n", "4 Damian Lillard makes two point jump shot (Josh... 11:31 twopointmade \n", ".. ... ... ... \n", "458 Dennis Schroder makes regular free throw 1 of 2 00:30 freethrowmade \n", "459 Dennis Schroder makes regular free throw 2 of 2 00:30 freethrowmade \n", "460 Damian Lillard misses three point pullup jump ... 00:24 threepointmiss \n", "461 LeBron James defensive rebound 00:21 rebound \n", "462 End of 4th Quarter. 00:00 endperiod \n", "\n", " home_points away_points quarter \n", "0 0 0 1 \n", "1 0 0 1 \n", "2 0 0 1 \n", "3 0 2 1 \n", "4 2 2 1 \n", ".. ... ... ... \n", "458 112 120 4 \n", "459 112 121 4 \n", "460 112 121 4 \n", "461 112 121 4 \n", "462 112 121 4 \n", "\n", "[463 rows x 10 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = json_files[0]\n", "\n", "# Get the play by play data from the file\n", "play_by_play_df = get_game_pbp_from_file(file)\n", "\n", "play_by_play_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Doing it on all files" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File ./json_files/play_by_play_sportsradar_69fd5274-20fe-494a-89c2-14988b791d31.json does not contain play-by-play data\n" ] } ], "source": [ "all_dfs = []\n", "\n", "# Loop through each JSON file\n", "for file_name in json_files:\n", " # Get the DataFrame for the current file\n", " play_by_play_df = get_game_pbp_from_file(file_name)\n", " # Append the DataFrame to the list\n", " all_dfs.append(play_by_play_df)\n", "\n", "# Concatenate all DataFrames into a single DataFrame\n", "final_df = pd.concat(all_dfs, ignore_index=True)\n", "\n", "# # Print the final DataFrame\n", "# print(final_df)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "193" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_df\n", "\n", "#check for unique dates\n", "\n", "len(final_df[\"id\"].unique())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatehome_team_nameaway_team_namedescriptionclockevent_typehome_pointsaway_pointsquarter
06decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersLakers lineup change (Thomas Bryant, LeBron Ja...12:00lineupchange001
16decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersTrail Blazers lineup change (Damian Lillard, J...12:00lineupchange001
26decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersThomas Bryant vs. Jusuf Nurkic (Dennis Schrode...12:00opentip001
36decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersLeBron James makes two point cutting dunk (Den...11:47twopointmade021
46decf9f6-cf39-4bf4-94b1-0acc349bfb692023-01-23T02:00:00+00:00Portland Trail BlazersLos Angeles LakersDamian Lillard makes two point jump shot (Josh...11:31twopointmade221
.................................
8896481509f8f-9014-460b-b383-70b65a0efdaf2023-03-20T01:00:00+00:00Portland Trail BlazersLA ClippersPaul George defensive rebound00:47rebound1021174
8896581509f8f-9014-460b-b383-70b65a0efdaf2023-03-20T01:00:00+00:00Portland Trail BlazersLA ClippersClippers turnover (shot clock violation)00:24turnover1021174
8896681509f8f-9014-460b-b383-70b65a0efdaf2023-03-20T01:00:00+00:00Portland Trail BlazersLA ClippersDamian Lillard misses three point pullup jump ...00:19threepointmiss1021174
8896781509f8f-9014-460b-b383-70b65a0efdaf2023-03-20T01:00:00+00:00Portland Trail BlazersLA ClippersIvica Zubac defensive rebound00:16rebound1021174
8896881509f8f-9014-460b-b383-70b65a0efdaf2023-03-20T01:00:00+00:00Portland Trail BlazersLA ClippersEnd of 4th Quarter.00:00endperiod1021174
\n", "

88969 rows × 10 columns

\n", "
" ], "text/plain": [ " id date \\\n", "0 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "1 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "2 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "3 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "4 6decf9f6-cf39-4bf4-94b1-0acc349bfb69 2023-01-23T02:00:00+00:00 \n", "... ... ... \n", "88964 81509f8f-9014-460b-b383-70b65a0efdaf 2023-03-20T01:00:00+00:00 \n", "88965 81509f8f-9014-460b-b383-70b65a0efdaf 2023-03-20T01:00:00+00:00 \n", "88966 81509f8f-9014-460b-b383-70b65a0efdaf 2023-03-20T01:00:00+00:00 \n", "88967 81509f8f-9014-460b-b383-70b65a0efdaf 2023-03-20T01:00:00+00:00 \n", "88968 81509f8f-9014-460b-b383-70b65a0efdaf 2023-03-20T01:00:00+00:00 \n", "\n", " home_team_name away_team_name \\\n", "0 Portland Trail Blazers Los Angeles Lakers \n", "1 Portland Trail Blazers Los Angeles Lakers \n", "2 Portland Trail Blazers Los Angeles Lakers \n", "3 Portland Trail Blazers Los Angeles Lakers \n", "4 Portland Trail Blazers Los Angeles Lakers \n", "... ... ... \n", "88964 Portland Trail Blazers LA Clippers \n", "88965 Portland Trail Blazers LA Clippers \n", "88966 Portland Trail Blazers LA Clippers \n", "88967 Portland Trail Blazers LA Clippers \n", "88968 Portland Trail Blazers LA Clippers \n", "\n", " description clock \\\n", "0 Lakers lineup change (Thomas Bryant, LeBron Ja... 12:00 \n", "1 Trail Blazers lineup change (Damian Lillard, J... 12:00 \n", "2 Thomas Bryant vs. Jusuf Nurkic (Dennis Schrode... 12:00 \n", "3 LeBron James makes two point cutting dunk (Den... 11:47 \n", "4 Damian Lillard makes two point jump shot (Josh... 11:31 \n", "... ... ... \n", "88964 Paul George defensive rebound 00:47 \n", "88965 Clippers turnover (shot clock violation) 00:24 \n", "88966 Damian Lillard misses three point pullup jump ... 00:19 \n", "88967 Ivica Zubac defensive rebound 00:16 \n", "88968 End of 4th Quarter. 00:00 \n", "\n", " event_type home_points away_points quarter \n", "0 lineupchange 0 0 1 \n", "1 lineupchange 0 0 1 \n", "2 opentip 0 0 1 \n", "3 twopointmade 0 2 1 \n", "4 twopointmade 2 2 1 \n", "... ... ... ... ... \n", "88964 rebound 102 117 4 \n", "88965 turnover 102 117 4 \n", "88966 threepointmiss 102 117 4 \n", "88967 rebound 102 117 4 \n", "88968 endperiod 102 117 4 \n", "\n", "[88969 rows x 10 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_df" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "#save it to parquet\n", "\n", "final_df.to_parquet(\"combined_dataframe.parquet\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Separate parquet files for each game\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File ./json_files/play_by_play_sportsradar_69fd5274-20fe-494a-89c2-14988b791d31.json does not contain play-by-play data\n" ] } ], "source": [ "import os\n", "\n", "# Loop through each JSON file\n", "for file_name in json_files:\n", " # Get the DataFrame for the current file\n", " play_by_play_df = get_game_pbp_from_file(file_name)\n", " \n", " # Extract home and away team names from the DataFrame\n", " if play_by_play_df is None:\n", " continue\n", "\n", "\n", " home_team = play_by_play_df['home_team_name'].iloc[0]\n", " away_team = play_by_play_df['away_team_name'].iloc[0]\n", " \n", " # Create the file name\n", " parquet_file_name = f\"{home_team}VS{away_team}.parquet\"\n", " \n", " # Save the DataFrame as a Parquet file\n", " play_by_play_df.to_parquet(os.path.join('Data/', parquet_file_name))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }