Women's college basketball — 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
| Parameter | Type | Default | Description |
|---|---|---|---|
rosters | dict[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.
classify_point_value
classify_point_value(dist_ft: 'float', x: 'float', y: 'float', *, league: 'str', season: 'int') -> 'int'
2 or 3 from basket-relative geometry (arc radius + corner band).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
dist_ft | float | Euclidean distance from the basket, feet. | |
x | float | Lateral offset from the basket, feet (baseline direction). | |
y | float | Distance up-court from the basket, feet. | |
league | str | "mens" or "womens". | |
season | int | Season-ending year (selects the arc era). |
Returns
3 at/beyond the arc or in the corner band, else 2.
Example
from sportsdataverse.mbb.mbb_shots_adapter import classify_point_value
classify_point_value(24.0, 0.0, 24.0, league="mens", season=2020)
classify_zone_geometry
classify_zone_geometry(dist_ft: 'float', x: 'float', y: 'float', *, league: 'str', season: 'int') -> 'str'
Shot zone from geometry: rim | paint | mid | corner3 | abovebreak3.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
dist_ft | float | Euclidean distance from the basket, feet. | |
x | float | Lateral offset from the basket, feet. | |
y | float | Distance up-court from the basket, feet. | |
league | str | "mens" or "womens". | |
season | int | Season-ending year (selects the arc era). |
Returns
One of rim, paint, mid, corner3, abovebreak3.
Example
from sportsdataverse.mbb.mbb_shots_adapter import classify_zone_geometry
classify_zone_geometry(2.0, 0.0, 2.0, league="mens", season=2020)
classify_zone_type
classify_zone_type(type_text: "'str | None'") -> "'str | None'"
Collapse a source shot-type label to rim | arc3 | jump.
Note: the 2025+ ESPN shots release carries NO three-point marker in
type_text (vocabulary is JumpShot/LayUpShot/DunkShot/TipShot), so
arc3 typically comes from geometry/score_value there; the branch
exists for sources that do label threes.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
type_text | str | None | Source label (e.g. "DunkShot"); None passes through. |
Returns
rim, arc3, jump, or None for null input.
Example
from sportsdataverse.mbb.mbb_shots_adapter import classify_zone_type
classify_zone_type("DunkShot")
espn_shots_to_canonical
espn_shots_to_canonical(espn: 'pl.DataFrame', *, league: 'str', season: 'int', scale: "'tuple[float, float, float] | None'" = None) -> 'pl.DataFrame'
ESPN load_mbb_shots frame -> the canonical shot frame.
Field-goal attempts only (free throws and sentinel-coordinate rows are
dropped). point_value comes from score_value -- the release
populates it on misses too, and its type_text carries NO three-point
marker, so arc3 is value-derived. Coordinates are re-based to the
fitted basket origin and scaled to feet.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
espn | DataFrame | load_mbb_shots-shaped frame. | |
league | str | "mens" or "womens". | |
season | int | Season-ending year. | |
scale | tuple[float, float, float] | None | None | Optional pre-fitted (origin_x, origin_y, feet_per_unit); fitted from espn when None. |
Returns
The canonical shot frame (CANONICAL_SHOT_SCHEMA); empty input returns the zero-row schema.
| col_name | type | description |
|---|---|---|
game_id | String | Unique game identifier. |
season | Int64 | Season identifier (4-digit year or 'YYYY-YY' string). |
team_id | String | Unique team identifier. |
shooter_id | String | Unique identifier for shooter. |
shot_x | Float64 | |
shot_y | Float64 | |
dist_ft | Float64 | |
shot_zone | String | |
shot_type | String | Shot type label (e.g. 'Jump Shot', 'Layup'). |
made | Boolean | |
point_value | Int64 | |
period | Int64 | Period of the game (1-4 quarters; 5+ for OT). |
sec_left | Float64 | |
source | String |
Example
from sportsdataverse.mbb.mbb_loaders import load_mbb_shots
from sportsdataverse.mbb.mbb_shots_adapter import espn_shots_to_canonical
df = espn_shots_to_canonical(load_mbb_shots([2025]), league="mens", season=2025)
fit_espn_court_scale
fit_espn_court_scale(espn: 'pl.DataFrame', *, league: 'str', season: 'int') -> "'tuple[float, float, float]'"
Fit the ESPN raw-coordinate court scale: (origin_x, origin_y, feet_per_unit).
The release's coordinate_{x,y}_raw grid is basket-anchored half-court
(width 0-50, rim cluster near (25, 2)). Origin = median raw
coordinates of made rim-type shots; feet_per_unit = arc radius /
median unit-distance of made threes from that origin -- fitted, not
guessed, so a units change in the release shows up as a scale shift.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
espn | DataFrame | load_mbb_shots-shaped frame. | |
league | str | "mens" or "womens". | |
season | int | Season-ending year (selects the arc radius). |
Returns
(origin_x, origin_y, feet_per_unit); documented fallbacks (25.0, 2.0, 1.0) when either calibration subset is empty.
Example
from sportsdataverse.mbb.mbb_shots_adapter import fit_espn_court_scale
scale = fit_espn_court_scale(espn, league="mens", season=2025)
infer_wbb_period_model
infer_wbb_period_model(html: 'str') -> "'tuple[int, int, int]'"
Tell a halves-era WBB page from a quarters-era one by its first-period clock.
stats.ncaa.org serves one table per period after the line score; the first period table's clock column runs down from 20:00 in a halves game and from 10:00 in a quarters game, so any clock above 600 seconds identifies the halves era. The line-score width alone cannot (halves + 2 OT and quarters
- 0 OT both have six columns).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
html | str | The raw play-by-play page. |
Returns
(2, 1200, 300) when a first-period clock above 10:00 is present, otherwise the quarters model (an unreadable page falls through to the modern model, matching ~sportsdataverse.scrape.ncaa.parse.wbb_period_model).
Example
from sportsdataverse.wbb.wbb_ncaa_game_pbp import infer_wbb_period_model
infer_wbb_period_model(open("pbp_1613299.html").read()) # (2, 1200, 300)
ncaa_wbb_game_pbp
ncaa_wbb_game_pbp(game_id: 'object', *, season: 'Optional[object]' = None, fetcher: 'Optional[_SupportsFetchGamePbp]' = None, return_as_pandas: 'bool' = False) -> "Union[pl.DataFrame, 'pd.DataFrame']"
Scrape one WBB game's play-by-play (wbigballR scrape_game, era-aware).
Same engine as sportsdataverse.mbb.mbb_ncaa_game_pbp.ncaa_mbb_game_pbp
with the WBB period model for the game's era bound: halves through 2014-15,
quarters from 2015-16 (see the module docstring).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
game_id | object | NCAA contest id (e.g. "5722355"). | |
season | Optional[object] | None | Season as an ending year (2015) or span ("2014-15"). When omitted, NaN or blank, the era is inferred from the page's first-period clock (infer_wbb_period_model). |
fetcher | Optional[_SupportsFetchGamePbp] | None | Optional injected fetcher exposing fetch_game_pbp (for tests/offline use). Defaults to a fresh NcaaFetcher.with_browser() context per call. |
return_as_pandas | bool | False | Return a pandas DataFrame instead of polars. |
Returns
The 35-column play-by-play frame (zero rows when the game is not found).
| col_name | type | description |
|---|---|---|
game_id | String | Unique game identifier. |
game_date | String | Game date (YYYY-MM-DD). |
home | String | Home. |
away | String | Away record. |
period | Int64 | Period of the game (1-4 quarters; 5+ for OT). |
clock | String | Game clock value. |
game_time | String | Game start time. |
game_seconds | Int64 | |
home_score | Int64 | Home team score at the time of the play. |
away_score | Int64 | Away team score at the time of the play. |
event_team | String | |
event_description | String | |
player_1 | String | |
player_2 | String | |
event_type | String | Event / play type code (V2 PBP). |
event_result | String | |
shot_value | Int64 | Point value of the shot (2 or 3). |
event_length | Int64 | |
poss_num | Int64 | |
poss_team | String | |
poss_length | Int64 | |
is_transition | Boolean | |
home_1 | String | |
home_2 | String | |
home_3 | String | |
home_4 | String | |
home_5 | String | |
away_1 | String | |
away_2 | String | |
away_3 | String | |
away_4 | String | |
away_5 | String | |
status | String | Status label. |
is_garbage_time | Boolean | |
sub_deviate | Int64 |
Raises
| Exception | Description |
|---|---|
ValueError | game_id is not an integral contest id (None, NaN, 5722355.5); a float such as 5722355.0 is accepted. |
Example
from sportsdataverse.wbb.wbb_ncaa_game_pbp import ncaa_wbb_game_pbp
df = ncaa_wbb_game_pbp("5722355")
print(df.shape)
# Pin the era explicitly for a 2014-15 (halves) game
df = ncaa_wbb_game_pbp("1613299", season=2015)
ncaa_wbb_play_by_play
ncaa_wbb_play_by_play(game_ids: 'Sequence[object]', *, season: 'Optional[object]' = None, fetcher: 'Optional[_SupportsFetchGamePbp]' = None, return_as_pandas: 'bool' = False, errors: "Literal['raise', 'skip']" = 'raise') -> "Union[pl.DataFrame, 'pd.DataFrame']"
Scrape many WBB games' play-by-play (wbigballR get_play_by_play, era-aware).
Same driver as sportsdataverse.mbb.mbb_ncaa_game_pbp.ncaa_mbb_play_by_play
(drop missing ids, shared fetcher session, one retry per empty or failed scrape
except a page the format guard rejected) with
the WBB period model for each game's era bound.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
game_ids | Sequence[object] | NCAA contest ids; None/NaN entries and non-integral floats (5722355.5) are dropped, an integral float (5722355.0) is read as "5722355". | |
season | Optional[object] | None | Season shared by every id, as an ending year or span. When omitted, NaN or blank, each page's era is inferred from its first-period clock, so ids from both eras may be mixed. |
fetcher | Optional[_SupportsFetchGamePbp] | None | Optional injected fetcher exposing fetch_game_pbp. Defaults to one shared NcaaFetcher.with_browser() context. |
return_as_pandas | bool | False | Return a pandas DataFrame instead of polars. |
errors | Literal['raise', 'skip'] | 'raise' | "raise" (default): a failed fetch (a ban, an exhausted proxy pool, an uncleared challenge) raises AssetFetchError. "skip": it is dropped like a missing game, and the frame gains a scrape_status column -- "ok" on every play row, plus one row per dropped game (game_id and why: fetch_failed: ..., no_data: ..., request_error: ..., parse_failed: ... or empty). |
Returns
Row-bound play-by-play for every game that scraped successfully (zero-row contract frame when none did).
| col_name | type | description |
|---|---|---|
game_id | String | Unique game identifier. |
game_date | String | Game date (YYYY-MM-DD). |
home | String | Home. |
away | String | Away record. |
period | Int64 | Period of the game (1-4 quarters; 5+ for OT). |
clock | String | Game clock value. |
game_time | String | Game start time. |
game_seconds | Int64 | |
home_score | Int64 | Home team score at the time of the play. |
away_score | Int64 | Away team score at the time of the play. |
event_team | String | |
event_description | String | |
player_1 | String | |
player_2 | String | |
event_type | String | Event / play type code (V2 PBP). |
event_result | String | |
shot_value | Int64 | Point value of the shot (2 or 3). |
event_length | Int64 | |
poss_num | Int64 | |
poss_team | String | |
poss_length | Int64 | |
is_transition | Boolean | |
home_1 | String | |
home_2 | String | |
home_3 | String | |
home_4 | String | |
home_5 | String | |
away_1 | String | |
away_2 | String | |
away_3 | String | |
away_4 | String | |
away_5 | String | |
status | String | Status label. |
is_garbage_time | Boolean | |
sub_deviate | Int64 |
Raises
| Exception | Description |
|---|---|
AssetFetchError | A fetch failed and errors="raise". |
ValueError | errors is not "raise" or "skip". |
Example
from sportsdataverse.wbb.wbb_ncaa_game_pbp import ncaa_wbb_play_by_play
df = ncaa_wbb_play_by_play(["5722355", "5732292"])
print(df.shape)
shot_events_to_frame
shot_events_to_frame(events: 'list[ShotEvent]', *, season: 'int', league: 'str' = 'mens') -> 'pl.DataFrame'
Flatten NCAA HTML ShotEvent objects to the canonical frame.
The NCAA SVG shot maps carry location + made/miss but no shot-type label
(shot_type = "unknown"); point_value/shot_zone come from the
geometry classifiers. The parser-phase pts field is the MADE flag
(1/0), not the point value.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
events | list[ShotEvent] | Parsed shot events (create_shot_event_data output). | |
season | int | Season-ending year the events belong to. | |
league | str | 'mens' | "mens" or "womens". |
Returns
The canonical shot frame (CANONICAL_SHOT_SCHEMA); empty input returns the zero-row schema.
| col_name | type | description |
|---|---|---|
game_id | String | Unique game identifier. |
season | Int64 | Season identifier (4-digit year or 'YYYY-YY' string). |
team_id | String | Unique team identifier. |
shooter_id | String | Unique identifier for shooter. |
shot_x | Float64 | |
shot_y | Float64 | |
dist_ft | Float64 | |
shot_zone | String | |
shot_type | String | Shot type label (e.g. 'Jump Shot', 'Layup'). |
made | Boolean | |
point_value | Int64 | |
period | Int64 | Period of the game (1-4 quarters; 5+ for OT). |
sec_left | Float64 | |
source | String |
Example
from sportsdataverse.mbb.mbb_shots_adapter import shot_events_to_frame
df = shot_events_to_frame(events, season=2025)
wbb_pbp_disk
wbb_pbp_disk(game_id, path_to_json)
Read a saved ESPN WBB play-by-play payload from disk.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
game_id | The ESPN game id; the file read is {game_id}.json. | ||
path_to_json | The directory holding the saved payloads. |
Returns
The payload exactly as saved (the raw ESPN summary JSON), ready for helper_wbb_pbp.
Raises
| Exception | Description |
|---|---|
FileNotFoundError | There is no {game_id}.json in path_to_json. |