Skip to main content

Adding players on court to play-by-play data

Project description

PyPI License: MIT Downloads Telegram Open In Colab

Fast download of play-by-play data and adding data about players on court in NBA games.

nba_on_court package allows you next things:

  1. Fast download play-by-play data from nba_data repository
  2. Add to play-by-play data information about players who were on court at any given time.
  3. Merge play-by-play data from different sources

Instalation

pip install nba-on-court

Tutorial

To understand work of library, you can study tutorials: in russian and english. There is also an interactive tutorial on Google Colab.

Open In Colab

Fast download play-by-play data from nba_data repository

With help of the previous version of the library, it was not possible to get play-by-play data, for this it was necessary to use third-party solutions, for example the library nba_api. The disadvantage of this approach is speed: NBA website has quite strict limits on the number of requests, so collecting play-by-play data from one season can take several hours.

nba_data repository, which containing play-by-play data from three sources (nba.stats.com , pbpstats.com , data.nba.com), as well as shotdetail for all games (regular season and playoffs) since the 1996/97 season (data from pbpstats.com and data.nba.com from the season of their appearance). Due to the fact that you just download a file from github, downloading one season of play-by-play data will take several seconds (depends on your internet speed). In 5-10 minutes, you can download the entire array of data for 28 seasons. Fast loading of play-by-play data is carried out using the load_nba_data function.

import nba_on_court as noc
noc.load_nba_data(seasons=2022, data='nbastats')

Add to play-by-play data information about players who were on court at any given time

Play-by-play NBA data contains information about each event in the game (throw, substitution, foul, etc.) and players who participated in it (PLAYER1_ID, PLAYER2_ID, PLAYER3_ID).

From this data, we get a list of players who were on court in this quarter. Then, we need to filter this list to 10 people who started quarter. This is done by analyzing substitutions in quarter.

players_on_court takes play-by-play data as input and returns it with 10 columns of the PLAYER_ID of players who were on court at each time.

players_name allows you to replace PLAYER_ID with first and last name of player. This allows user to understand exactly which players were on court (few know PLAYER_ID all players in NBA),but it is not necessary to do this before calculations, because the player's NAME_SURNAME is not unique, unlike PLAYER_ID.

import nba_on_court as noc
from nba_api.stats.endpoints import playbyplayv2

pbp = playbyplayv2.PlayByPlayV2(game_id="0022100001").play_by_play.get_data_frame()
pbp_with_players = noc.players_on_court(pbp)
len(pbp_with_players.columns) - len(pbp.columns)
10

players_id = list(pbp_with_players.iloc[0, 34:].reset_index(drop=True))
print(players_id)
[201142, 1629651, 201933, 201935, 203925, 201572, 201950, 1628960, 203114, 203507]

players_name = noc.players_name(players_id)
print(players_name)
['Kevin Durant', 'Nic Claxton', 'Blake Griffin', 'James Harden', 'Joe Harris',
 'Brook Lopez', 'Jrue Holiday', 'Grayson Allen', 'Khris Middleton', 'Giannis Antetokounmpo']

You can also replace the PLAYER_ID with the player's name in the entire data frame at once.

    cols = ["PLAYER1", "PLAYER2", "PLAYER3", "PLAYER4", "PLAYER5", "PLAYER6", "PLAYER7", "PLAYER8", "PLAYER9", "PLAYER10"]
    pbp_with_players.loc[:, cols] = pbp_with_players.loc[:, cols].apply(noc.players_name, result_type="expand")

Merge play-by-play data from different sources

Sometimes you need to combine data from different sources to solve a problem. For example, we want to find out how the on/off of a partner on the floor affects the player's shot selection. To do this, we need detailed throw data (where there are coordinates and throw zones), as well as play-by-play data with information about the presence on court in order to divide the throws according to condition. In the repository nba_data 3 data sources (nba.stats, data.stats and shotdetail) have a single source: NBA website. Therefore, it is quite easy to combine them by two keys (the name of the columns differs in different sources):

  • Game ID
  • Event ID

With data from pbpstats.com more complicated: they initially have a another structure (grouped by possessions), so they do not have an event ID. At the same time, they contain useful information that is not explicitly available in other sources (time of possessions, type of possessions start, url of video episode). The only way to combine them is to use the event DESCRIPTION. The problem here is that the descriptions in nba.stats and pbpstats also do not match and an attempt to merge them directly will lead to the loss of a certain number of rows.

To solve this problem, I created the functions left_join_nbastats and left_join_pbpstats. They allow you to merge play-by-play data with NBA.stats and pbpstats with almost no errors.

import pandas as pd
import nba_on_court as noc

noc.load_nba_data(seasons=2022, data=('nbastats', 'pbpstats'), seasontype='po', untar=True)

nbastats = pd.read_csv('nbastats_po_2022.csv')
pbpstats = pd.read_csv('pbpstats_po_2022.csv')

nbastats = nbastats.loc[nbastats['GAME_ID'] == 42200405].reset_index(drop=True)
pbpstats = pbpstats.loc[pbpstats['GAMEID'] == 42200405].reset_index(drop=True)

print(nbastats.shape, pbpstats.shape)
((463, 34), (396, 19))

full_pbp = noc.left_join_nbastats(nbastats, pbpstats)
print(full_pbp.shape)
(463, 50)

Contact me:

If you have questions or proposal about dataset, you can write me convenient for you in a way.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nba_on_court-0.2.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

nba_on_court-0.2.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file nba_on_court-0.2.0.tar.gz.

File metadata

  • Download URL: nba_on_court-0.2.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.4 Linux/5.0.0-1068-oem-osp1

File hashes

Hashes for nba_on_court-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ab5bda44ebacfa68bbce3b5e515b085ca126237e105a839b0b69bd59b037b353
MD5 00bc136aa379368970b6b455d4ec8c17
BLAKE2b-256 c26ac17f7c136892ef6280bb99affbbfd1db39f251fb3ff83f68079e6d41201a

See more details on using hashes here.

File details

Details for the file nba_on_court-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nba_on_court-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.4 Linux/5.0.0-1068-oem-osp1

File hashes

Hashes for nba_on_court-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa9eb465d373837da476e876d767398f82694f1ece91e5b16b14bf2a52afcc8a
MD5 4317917afdb6e37cdebdad5584b409ec
BLAKE2b-256 a2b250412e1cdb100f85ab6242da9a979cf2b7dc9c2b22d26516c61e0707e472

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page