Skip to main content
Version: 0.2.0

NBA — additional Python functions — Play-by-play processing

build_athlete_identity_lookup​

build_athlete_identity_lookup(rosters: 'dict[int | str, dict]') -> 'dict[str, dict[str, Any]]'

R build_athlete_identity_lookup: athlete_id -> identity from team rosters.

Parameters

ParameterTypeDefaultDescription
rostersdict[int | str, dict]Mapping of team_id -> that team's raw roster payload (wbb/team_rosters/json/{season}/{team_id}.json). NOTE: R walks raw$athletes directly here (no position-bucket unwrap, unlike the rosters dataset itself).

Returns

athlete_id (str) -> identity fields for helper_wbb_player_season_stats.

build_nba_player_identity_lookup​

build_nba_player_identity_lookup(player_box: 'pl.DataFrame') -> 'dict[str, dict[str, Any]]'

R build_identity_lookup(season): athlete_id -> identity from the

season's already-compiled player_box -- the authoritative "who played in season Y" source (ESPN's team-roster endpoint is current-only and cannot answer that for historical seasons).

Parameters

ParameterTypeDefaultDescription
player_boxDataFrameThe season's compiled player_box frame (e.g. nba/player_box/parquet/player_box_{season}.parquet, or whatever the season builder just wrote for this pass). Must carry athlete_id; other identity columns are best-effort.

Returns

athlete_id (str) -> identity fields for helper_nba_player_season_stats. When an athlete appears in multiple rows (multiple games), the row of the LATEST game_date wins (frame order breaks ties and decides when there is no game_date) -- R's !duplicated(athlete_id, fromLast = TRUE), which keeps an athlete's most recent team within the season. The published player_box is written newest game first, so frame order alone kept a traded athlete's FIRST team.

nba_pbp_disk​

nba_pbp_disk(game_id, path_to_json)

Load a previously cached ESPN NBA summary JSON for a game from disk.

Reads {path_to_json}/{game_id}.json.

Parameters

ParameterTypeDefaultDescription
game_idintESPN game / event identifier.
path_to_jsonstrDirectory containing the cached JSON file.

Returns

Parsed JSON contents.

Example

from sportsdataverse.nba import nba_pbp_disk
pbp = nba_pbp_disk(game_id=401585183, path_to_json="./cache")
print(list(pbp.keys()))

nba_v3_to_v2_pbp​

nba_v3_to_v2_pbp(pbp_v3: 'dict', box_v3: 'dict', *, return_as_pandas: 'bool' = False) -> 'Union[pl.DataFrame, pd.DataFrame]'

Convert a v3 playbyplayv3 payload into the full v2-schema pbp frame.

Ports hoopR's .v3_to_v2_format() (R/nba_stats_pbp.R lines 210-810) to polars: the v3 feed (stats.nba.com playbyplayv3) is reshaped into the older v2 schema that the committed hoopR-nba-stats-data dataset carries and that pbpstats' stats_nba provider consumes. This is a pure, network-free function -- both payloads must already be fetched (e.g. via nba_stats_playbyplayv3 / nba_stats_boxscoretraditionalv3).

Pipeline:

  1. Build the per-person_id roster from box_v3 (_build_roster) and recover player2_id/player3_id (assist/block/steal/sub-in/jump) from pbp_v3 ( _extract_secondary_players).
  2. Drop the standalone block/steal rows consolidated into their parent Missed Shot / Turnover (_is_dropped_block_steal) -- the only row-count change versus the raw v3 action list.
  3. Derive event_type/event_action_type from the module's lookup tables, split description by location into home/visitor/ neutral, forward-fill the running score, and enrich player2/ player3 from the roster by id (see _secondary_fields for the deliberate divergence from hoopR's name-based re-resolution).

Parameters

ParameterTypeDefaultDescription
pbp_v3dictRaw playbyplayv3 dict (nba_stats_playbyplayv3 / wnba_stats_playbyplayv3 payload shape); actions live at pbp_v3["game"]["actions"].
box_v3dictRaw boxscoretraditionalv3 dict, passed through to _build_roster.
return_as_pandasboolFalseIf True, return a pandas.DataFrame instead of polars.DataFrame.

Returns

Polars (or pandas) DataFrame with the full v2 schema (game/event identifiers, event/action type codes, home/visitor/neutral descriptions, forward-filled score + margin + leader, per-player columns for players 1-3, and the v3 passthrough columns). Empty or malformed input returns a zero-row frame with the same schema (never raises). Malformed or empty input returns a zero-row frame with the documented schema instead of raising.

col_nametypedescription
game_idStringUnique game identifier.
event_numStringSequential event number within the game (V2 PBP).
event_typeStringEvent / play type code (V2 PBP).
event_action_typeStringNumeric event-action-type code (V2 PBP).
periodInt64Period of the game (1-4 quarters; 5+ for OT).
clockStringGame clock value.
minute_gameFloat64Minute game.
time_remainingFloat64Time remaining.
wc_time_stringStringWc time string.
time_quarterStringTime quarter.
minute_remaining_quarterInt64Minute remaining quarter.
seconds_remaining_quarterInt64Seconds remaining quarter.
action_typeStringAction type label (e.g. 'Made Shot', 'Substitution').
sub_typeStringAction sub-type label.
home_descriptionStringHome team's description.
neutral_descriptionStringNeutral description.
visitor_descriptionStringVisitor description.
descriptionStringLong-form description text.
locationStringLocation.
scoreStringFinal score.
away_scoreInt64Away team score at the time of the play.
home_scoreInt64Home team score at the time of the play.
score_marginStringScore margin.
team_leadingStringTeam leading.
person1typeStringPerson1type.
player1_idStringV2 PBP primary player ID (e.g. shooter / fouler).
player1_nameStringV2 PBP primary player name.
player1_team_idStringTeam ID of player1.
player1_team_cityStringPlayer1 team city.
player1_team_nicknameStringPlayer1 team nickname.
player1_team_abbreviationStringPlayer1 team abbreviation.
person2typeStringPerson2type.
player2_idStringV2 PBP secondary player ID (e.g. assister / fouled-by).
player2_nameStringV2 PBP secondary player name.
player2_team_idStringTeam ID of player2.
player2_team_cityStringPlayer2 team city.
player2_team_nicknameStringPlayer2 team nickname.
player2_team_abbreviationStringPlayer2 team abbreviation.
person3typeStringPerson3type.
player3_idStringV2 PBP tertiary player ID (e.g. blocker).
player3_nameStringV2 PBP tertiary player name.
player3_team_idStringTeam ID of player3.
player3_team_cityStringPlayer3 team city.
player3_team_nicknameStringPlayer3 team nickname.
player3_team_abbreviationStringPlayer3 team abbreviation.
video_available_flagStringVideo available flag.
x_legacyInt64V2-format X coordinate (preserved for V3-to-V2 compatibility).
y_legacyInt64V2-format Y coordinate (preserved for V3-to-V2 compatibility).
shot_distanceInt64Shot distance from the basket, in feet.
shot_resultStringShot result ('Made' / 'Missed').
is_field_goalInt641 if the action was a field goal; 0 otherwise.
points_totalInt64Running total of points scored.
shot_valueInt64Point value of the shot (2 or 3).
action_numberInt64Sequential action number within a game (V3 PBP).
team_idInt64Unique team identifier.
team_tricodeStringThree-letter team code (e.g. 'LAS' / 'NYL').
person_idInt64Unique player identifier (V3 endpoints).
player_nameStringPlayer name.
score_homeStringScore home.
score_awayStringScore away.
action_idInt64Unique action identifier within a game (V3 PBP).

Example

from sportsdataverse.nba.nba_v3_v2_adapter import nba_v3_to_v2_pbp
from sportsdataverse.nba.nba_stats import nba_stats_playbyplayv3, nba_stats_boxscoretraditionalv3

pbp_v3 = nba_stats_playbyplayv3(game_id="0022300001", return_parsed=False)
box_v3 = nba_stats_boxscoretraditionalv3(game_id="0022300001", return_parsed=False)
df = nba_v3_to_v2_pbp(pbp_v3, box_v3)
print(df.shape, df.columns)

# Pandas output

df_pd = nba_v3_to_v2_pbp(pbp_v3, box_v3, return_as_pandas=True)
print(type(df_pd))

# Pipeline next step (feed a pbpstats-style consumer)

df.filter(pl.col("event_type") == "1").select("player1_name", "player2_name")