College baseball — additional Python functions
Hand-written wrappers, loaders, and helpers in sportsdataverse.baseball.college_baseball that are not
generated API-endpoint wrappers (those have their own reference pages).
ESPN
espn_college_baseball_player_box
espn_college_baseball_player_box(game_id: 'int | str', return_as_pandas: 'bool' = False, **kwargs: 'Any') -> 'pl.DataFrame | pd.DataFrame'
Pull one college baseball game's player box score from ESPN's summary endpoint.
The baseballr table of the same name (baseballr::espn_college_baseball_player_box): one row
per athlete and side, batters then pitchers for the first team listed, then the second. One
request.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
game_id | int | str | ESPN game (event) id, from espn_college_baseball_scoreboard. | |
return_as_pandas | bool | False | Return a pandas frame instead of polars (pyarrow-backed columns, so the nullable ids stay integers). |
Returns
The columns of sportsdataverse.mlb.espn_mlb_player_box. A zero-row frame with those columns when ESPN lists no athletes for the game, as it does for many smaller games (baseballr returns NULL there).
| col_name | type | description |
|---|---|---|
game_id | Int64 | ESPN game (event) id. |
season | Int32 | Season (four-digit year) of the game, from the summary header. |
season_type | Int32 | ESPN season type of the game: 1 preseason, 2 regular season, 3 postseason (MLB); ESPN files the college postseason under other types (the 2024 Men's College World Series final is 6). |
game_date | Date | US/Eastern calendar date of the scheduled first pitch. |
game_date_time | Datetime | Scheduled first pitch, US/Eastern. |
stat_group | String | Which side of the box score the row reports: batting or pitching. A two-way player has one row for each. |
team_id | Int64 | ESPN team id of the athlete's team. |
team_name | String | Team nickname, e.g. Cardinals. |
team_abbreviation | String | Team abbreviation, e.g. STL. |
team_display_name | String | Full team name, e.g. St. Louis Cardinals. |
athlete_id | Int64 | ESPN athlete id of the player the row reports. |
athlete_display_name | String | Athlete's full name. |
athlete_short_name | String | Athlete's short name, e.g. N. Arenado. |
athlete_position_name | String | Position the athlete played in the game, e.g. Third Base or Pitcher. |
athlete_position_abbreviation | String | Abbreviation of the position the athlete played, e.g. 3B or P. |
starter | Boolean | True when the athlete started the game (on the batting side, was in the starting lineup). |
bat_order | Int32 | Spot in the batting order, 1-9; a substitute takes the spot of the player he replaced. Null on pitching rows. |
active | Boolean | ESPN's active flag for the athlete's box-score entry. |
h_ab | String | Batting: hits and at-bats as text, e.g. 2-4. |
ab | String | Batting: at-bats. |
r | String | Runs: scored by the batter on a batting row, allowed by the pitcher on a pitching row. |
h | String | Hits: by the batter on a batting row, allowed by the pitcher on a pitching row. |
rbi | String | Batting: runs batted in. |
hr | String | Home runs: hit by the batter on a batting row, allowed by the pitcher on a pitching row. |
bb | String | Walks: drawn by the batter on a batting row, issued by the pitcher on a pitching row. |
k | String | Strikeouts: by the batter on a batting row, by the pitcher on a pitching row. |
number_p | String | Batting: pitches the batter saw (ESPN's #P). |
avg | String | Batting: the batter's season batting average through the game, ESPN's display text (e.g. .286). |
obp | String | Batting: the batter's season on-base percentage through the game. |
slg | String | Batting: the batter's season slugging percentage through the game. |
ip | String | Pitching: innings pitched, in ESPN's thirds notation (6.1 = six and one third). |
er | String | Pitching: earned runs allowed. |
pc_st | String | Pitching: pitches and strikes thrown, e.g. 95-58. |
era | String | Pitching: the pitcher's season ERA through the game. |
pc | String | Pitching: pitches thrown. |
Raises
| Exception | Description |
|---|---|
NoDataError | ESPN answered 404 for the game. |
AssetFetchError | The request failed (a non-2xx answer or a connection failure after retries). |
Example
from sportsdataverse.baseball.college_baseball import espn_college_baseball_player_box
player_box = espn_college_baseball_player_box(game_id=401675653)
print(player_box.shape)
# Pipeline next step (one line)
import polars as pl
player_box.filter(pl.col("stat_group") == "batting").select("athlete_display_name", "h_ab", "rbi")
espn_college_baseball_team_box
espn_college_baseball_team_box(game_id: 'int | str', return_as_pandas: 'bool' = False, **kwargs: 'Any') -> 'pl.DataFrame | pd.DataFrame'
Pull one college baseball game's team box score from ESPN's summary endpoint.
The baseballr table of the same name (baseballr::espn_college_baseball_team_box): one row
per team with the game header, the team's identity, every batting, pitching and fielding stat
ESPN reports for the game, and the opponent's identity and score. One request.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
game_id | int | str | ESPN game (event) id, from espn_college_baseball_scoreboard. | |
return_as_pandas | bool | False | Return a pandas frame instead of polars (pyarrow-backed columns, so the nullable ids stay integers). |
Returns
Two rows, one per team, with the columns of sportsdataverse.mlb.espn_mlb_team_box. ESPN's college box carries fewer stats (about 70 batting_* / pitching_* / fielding_* columns) and none at all for many smaller games, which then have only the identity, score and opponent columns. A zero-row frame with the documented columns when ESPN has no box score for the game.
| col_name | type | description |
|---|---|---|
game_id | Int64 | ESPN game (event) id. |
season | Int32 | Season (four-digit year) of the game, from the summary header. |
season_type | Int32 | ESPN season type of the game: 1 preseason, 2 regular season, 3 postseason (MLB); ESPN files the college postseason under other types (the 2024 Men's College World Series final is 6). |
game_date | Date | US/Eastern calendar date of the scheduled first pitch. |
game_date_time | Datetime | Scheduled first pitch, US/Eastern. |
team_id | Int64 | ESPN team id of the team the row reports. |
team_uid | String | ESPN universal team id (UID), e.g. s:1 |
team_slug | String | URL slug of the team, e.g. st-louis-cardinals. |
team_location | String | Team city or school, e.g. St. Louis. |
team_name | String | Team nickname, e.g. Cardinals. |
team_abbreviation | String | Team abbreviation, e.g. STL. |
team_display_name | String | Full team name, e.g. St. Louis Cardinals. |
team_short_display_name | String | Short team name, e.g. Cardinals. |
team_color | String | Team primary color, hex without the leading #. |
team_alternate_color | String | Team alternate color, hex without the leading #. |
team_logo | String | URL of the team logo. |
batting_hit_by_pitch | String | Team batting: Hit By Pitch (HBP). The number of times a player has been by a pitch. ESPN's display text. |
batting_strikeouts | String | Team batting: Strikeouts (SO). The number of times a player strikes out. ESPN's display text. |
batting_rb_is | String | Team batting: Runs Batted In (RBI). The number of runs scored by a batter's play. ESPN's display text. |
batting_sac_hits | String | Team batting: Sacrifice Hit (SH). The number of sacrifice bunts that allow baserunners to advance. ESPN's display text. |
batting_hits | String | Team batting: Hits (H). The number of hits. ESPN's display text. |
batting_stolen_bases | String | Team batting: Stolen Bases (SB). The number of bases stolen. ESPN's display text. |
batting_walks | String | Team batting: Walks (BB). The number of times a batter reaches base on four balls. ESPN's display text. |
batting_runs | String | Team batting: Runs (R). The number of runs scored. ESPN's display text. |
batting_gid_ps | String | Team batting: Ground Into Double Play (GIDP). The number of times a batter grounds into a double play. ESPN's display text. |
batting_sac_flies | String | Team batting: Sacrifice Flies (SF). The number of fly outs on which a runner scored (a sacrifice fly). ESPN's display text. |
batting_at_bats | String | Team batting: At Bats (AB). The number of times a batter comes up to bat not counting walks and sacrifices. ESPN's display text. |
batting_home_runs | String | Team batting: Home Runs (HR). The number of hits that allowed the batter to round the bases and score without the aid of an error of the defense. ESPN's display text. |
batting_grand_slam_home_runs | String | Team batting: Grand Slam Home Runs (GSHR). The number of hits that allowed the batter and 3 of his teammates to round the bases and score without the aid of an error of the defense. ESPN's display text. |
batting_runners_left_on_base | String | Team batting: Runners Left On Base (LOB). The number runners remain on base after the batter makes an out at the plate. ESPN's display text. |
batting_triples | String | Team batting: Triples (3B). The number of hits that result in the batter reaching 3rd base without the aid of an error. ESPN's display text. |
batting_doubles | String | Team batting: Doubles (2B). The number of times that a batter reaches second base as the result of a hit without an error being committed. ESPN's display text. |
batting_caught_stealing | String | Team batting: Caught Stealing (CS). The number of times that a baserunner attempts to steal a base but is thrown out. ESPN's display text. |
batting_pitches | String | Team batting: Pitches (P). The number of pitches faced. ESPN's display text. |
batting_total_bases | String | Team batting: Total Bases (TB). The number of bases calculated as 1 for a 1B, 2 for a 2B, 3 for a 3B and 4 for a HR. ESPN's display text. |
batting_extra_base_hits | String | Team batting: Extra Base Hits (XBH). The number of hits that are greater than singles. ESPN's display text. |
batting_avg | String | Team batting: Batting Average (AVG). The average number of times the batter reached successfully on base due to a hit: hits / atBats. ESPN's display text. |
batting_slug_avg | String | Team batting: Slugging Percentage (SLG). Slugging percentage is a metric that is meant to measuring how many bases the hitter safely advances to after making contact with the ball: (totalBases / atBats). ESPN's display text. |
batting_on_base_pct | String | Team batting: On Base Percentage (OBP): (hits + walks + hit by pitch) / (at bats + walks + hit by pitch + sacrifice flies). ESPN's display text. |
batting_bipa | String | Team batting: Balls In Play Average (BIPA). The average of a batter when the ball is put in play or batting average without factoring at bats that result in a home run or a strikeout. ESPN's display text. |
batting_mlb_rating | String | Team batting: MLB Rating (MLB). The MLB rating. ESPN's display text. |
pitching_saves | String | Team pitching: Saves (SV). The number of times a pitcher enters and finishes the game without surrendering the lead in a save opportunity. ESPN's display text. |
pitching_strikeouts | String | Team pitching: Strikeouts (K). The number of times the pitcher struck out a batter. ESPN's display text. |
pitching_sac_flies | String | Team pitching: Sacrifice Flies (SF). The number of sacrifice flies surrendered. ESPN's display text. |
pitching_hits | String | Team pitching: Hits (H). The number of hits surrendered by a pitcher. ESPN's display text. |
pitching_earned_runs | String | Team pitching: Earned runs (ER). The number of earned runs surrendered by a pitcher. ESPN's display text. |
pitching_walks | String | Team pitching: Walks (BB). The number of times a pitcher surrenders a base on balls. ESPN's display text. |
pitching_runs | String | Team pitching: Runs (R). The number of runs allowed by a pitcher. ESPN's display text. |
pitching_stolen_bases | String | Team pitching: Stolen Bases (SB). The number of times a pitcher allows a baserunner to steal a base. ESPN's display text. |
pitching_caught_stealing | String | Team pitching: Caught Stealing (CS). The number of times a pitcher gets out a baserunner trying to steal a base. ESPN's display text. |
pitching_sac_bunts | String | Team pitching: Sacrifice Bunts (SH). The number of times a sacrifce bunt is allowed by a pitcher. ESPN's display text. |
pitching_save_opportunities | String | Team pitching: Save Opportunities (SVOP). The number of times a pitcher enters the game with a lead of fewer than 4 runs and pitches at least one inning; enters the game with the potentional tying run on base, at bat, or on deck; or pitches three or more innings with a lead. ESPN's display text. |
pitching_home_runs | String | Team pitching: Home Runs (HR). The number of times a pitcher has allowed a home run. ESPN's display text. |
pitching_triples | String | Team pitching: Triples (3B). The number of triples surrendered by the picher. ESPN's display text. |
pitching_doubles | String | Team pitching: Doubles (2B). The number of doubles allowed by a pitcher. ESPN's display text. |
pitching_rb_is | String | Team pitching: Runs Batted In (RBI). The number of runs scored due to the actions of the batter. ESPN's display text. |
pitching_third_innings | String | Team pitching: Third Innings Pitched (TIP). The number of third innings pitched. ESPN's display text. |
pitching_pitches | String | Team pitching: Pitches (P). The number of pitches a pitcher throws. ESPN's display text. |
pitching_at_bats | String | Team pitching: At Bats (AB). The number of at bats. ESPN's display text. |
pitching_full_innings | String | Team pitching: Full Innings (FI). The number of full innings pitched by a pitcher. ESPN's display text. |
pitching_part_innings | String | Team pitching: Partial Innings (PI). The number of times a pitcher pitches a part of an inning. ESPN's display text. |
pitching_era | String | Team pitching: Earned Run Average (ERA). The average number of runs a pitcher yields per 9 innings thrown. ESPN's display text. |
pitching_whip | String | Team pitching: Walks Plus Hits Per Inning Pitched (WHIP). The number representing walks plus hits divided by innings pitched. ESPN's display text. |
pitching_opponent_avg | String | Team pitching: Opponent Batting Average (OBA). The opponent's batting average for a pitcher. ESPN's display text. |
pitching_bipa | String | Team pitching: Balls In Play Average (BIPA). The batter's batting average when facing the pitcher without taking into account home runs or strikeouts. ESPN's display text. |
pitching_total_bases | String | Team pitching: Total Bases (TB). The number of bases allowed by a pitcher. ESPN's display text. |
pitching_pitch_count | String | Team pitching: Pitch Count (PC). The number of pitches thrown. ESPN's display text. |
pitching_strikes | String | Team pitching: Strikes (S). The number of strikes thrown. ESPN's display text. |
pitching_strike_pitch_ratio | String | Team pitching: Strike To Pitch Ratio (S/P). The ratio of strikes thrown to pitches thrown. ESPN's display text. |
fielding_double_plays | String | Team fielding: Double Plays (DP). The number of double plays a fielder has been a part of as either the assister or putouter. ESPN's display text. |
fielding_opportunities | String | Team fielding: Opportunities (OPP). The number of opportunites. ESPN's display text. |
fielding_errors | String | Team fielding: Errors (E). The number of times a fielder fails to make the play they are expected to make. ESPN's display text. |
fielding_passed_balls | String | Team fielding: Passed Balls (PB). The number of times the ball is dropped or passes by the catcher. ESPN's display text. |
fielding_assists | String | Team fielding: Assists (A). The number of times a fielder is involved in an out when the fielder is not the putouter. ESPN's display text. |
fielding_outfield_assists | String | Team fielding: Outfield Assists (OA). The number of times an outfielder fielder is involved in an out when the fielder is not the putouter. ESPN's display text. |
fielding_pickoffs | String | Team fielding: Pickoffs (PO). The number of times that a pitcher or catcher throws over and picks off a baserunner. ESPN's display text. |
fielding_putouts | String | Team fielding: Putouts (PO). The number of times the fielder tags or forces the out of a baserunner. ESPN's display text. |
fielding_outs_on_field | String | Team fielding: Outs On Field (OOF). The number of outs on field. ESPN's display text. |
fielding_triple_plays | String | Team fielding: TP. The number of times that a fielder is involved in a triple play. ESPN's display text. |
fielding_balls_in_zone | String | Team fielding: Balls In Zone (BIZ). The number of balls that were batted into a fielder's zone. ESPN's display text. |
fielding_outs_made | String | Team fielding: Outs Made (OM). The number of outs made by a fielder. ESPN's display text. |
fielding_hits | String | Team fielding: Hits (H). The number of hits surrendered. ESPN's display text. |
fielding_total_bases | String | Team fielding: Total Bases (TB). The number of bases allowed. ESPN's display text. |
fielding_catcher_caught_stealing | String | Team fielding: Caught Stealing (CS). The number of times a catcher throws a baserunner trying to steal a base. ESPN's display text. |
home_away | String | Whether the team was the home or the away side (home / away). |
team_score | Int32 | Runs the team scored in the game. |
team_winner | Boolean | True when the team won the game. |
opponent_team_id | Int64 | ESPN team id of the opponent. |
opponent_team_location | String | Opponent city or school. |
opponent_team_name | String | Opponent nickname. |
opponent_team_abbreviation | String | Opponent abbreviation. |
opponent_team_display_name | String | Full name of the opponent, e.g. Chicago Cubs. |
opponent_team_logo | String | URL of the opponent's logo. |
opponent_team_color | String | Opponent primary color, hex without the leading #. |
opponent_team_alternate_color | String | Opponent alternate color, hex without the leading #. |
opponent_team_score | Int32 | Runs the opponent scored in the game. |
Raises
| Exception | Description |
|---|---|
NoDataError | ESPN answered 404 for the game. |
AssetFetchError | The request failed (a non-2xx answer or a connection failure after retries). |
Example
from sportsdataverse.baseball.college_baseball import espn_college_baseball_team_box
team_box = espn_college_baseball_team_box(game_id=401675653)
print(team_box.shape)
# Pipeline next step (one line)
team_box.select("team_abbreviation", "batting_hits", "team_score", "team_winner")
stats.ncaa.org
college_baseball_re24
college_baseball_re24(seasons: 'Union[int, List[int], None]' = None, *, state: 'Optional[pl.DataFrame]' = None, return_as_pandas: 'bool' = False) -> "Union[pl.DataFrame, 'pd.DataFrame']"
sportsdataverse.baseball.college_run_expectancy.college_baseball_re24 fixed to league="college_baseball".
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
seasons | Union[int, List[int], None] | None | See the core function. |
state | Optional[DataFrame] | None | See the core function. |
return_as_pandas | bool | False | Return pandas.DataFrame instead of polars. |
Returns
see the core function's Returns table.
| col_name | type | description |
|---|---|---|
base_state | String | 3-char base occupancy code ("_" = empty, "1"/"2"/"3" = occupied), e.g. "1_3" for runners on first and third. |
outs | Int64 | Outs at the start of the base-out state (0-2). |
run_expectancy | Float64 | Empirical mean runs scored from this state through the end of the half-inning (RE24). |
n | Int64 | Number of plate appearances observed starting in this base-out state. |
Example
from sportsdataverse.baseball.college_baseball.college_baseball_re import college_baseball_state, college_baseball_re24
state = college_baseball_state(raw)
matrix = college_baseball_re24(state=state)
college_baseball_state
college_baseball_state(plays: 'Dict[str, Any]') -> 'pl.DataFrame'
sportsdataverse.baseball.college_run_expectancy.college_baseball_state fixed to league="college_baseball".
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
plays | Dict[str, Any] | Raw payload from espn_college_baseball_game_plays(event_id, return_parsed=False). |
Returns
see the core function's Returns table.
| col_name | type | description |
|---|---|---|
game_id | String | |
inning | Int64 | |
half | String | |
base_state | String | |
outs | Int64 | |
runs_before | Int64 | |
runs_after | Int64 | |
batting_team_id | String | |
play_seq | Int64 | |
score_diff | Int64 |
Example
from sportsdataverse.baseball.college_baseball.college_baseball_re import college_baseball_state
state = college_baseball_state(raw)
college_baseball_wpa
college_baseball_wpa(seasons: 'Union[int, List[int], None]' = None, *, state: 'Optional[pl.DataFrame]' = None, results: 'Optional[pl.DataFrame]' = None, return_as_pandas: 'bool' = False) -> "Union[pl.DataFrame, 'pd.DataFrame']"
sportsdataverse.baseball.college_run_expectancy.college_baseball_wpa fixed to league="college_baseball".
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
seasons | Union[int, List[int], None] | None | See the core function. |
state | Optional[DataFrame] | None | See the core function. |
results | Optional[DataFrame] | None | See the core function. |
return_as_pandas | bool | False | Return pandas.DataFrame instead of polars. |
Returns
see the core function's Returns table.
| col_name | type | description |
|---|---|---|
game_id | String | ESPN event id for the game (join key to the schedule). |
play_seq | Int64 | Game-global sequential plate-appearance order. |
re_before | Float64 | RE24 of the base-out state before the PA. |
re_after | Float64 | RE24 of the base-out state after the PA. |
run_value | Float64 | re_after minus re_before, plus runs scored on the play. |
wpa | Float64 | Home-perspective win-probability added. |
Example
from sportsdataverse.baseball.college_baseball.college_baseball_re import college_baseball_wpa
wpa = college_baseball_wpa(state=state, results=results)
Play-by-play processing
decompose_college_baseball_plays
decompose_college_baseball_plays(rows: "'list[dict]'", *, return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"
Decompose pre-extracted play rows into the full PBP_SCHEMA frame.
The row-level half of parse_college_baseball_ncaa_pbp -- the play-text
decomposition engine without the HTML extraction. This is the entry point
for sources that already hold the base play fields, e.g. the legacy R-era
baseballr-data trees (2012-2023: description/inning/
inning_top_bot/batting/fielding/score), so legacy and
freshly captured games resolve into IDENTICAL pbp columns.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
rows | list[dict] | One dict per play. Recognized keys (all optional except description): contest_id, inning (int), inning_top_bot ("top"/"bot"), batting, fielding, play_number, score_away/score_home (ints) or a combined score string ("3-2", away-home), and description. Unrecognized keys are ignored; play_number defaults to the 1-based position in rows. | |
return_as_pandas | bool | False | Return a pandas.DataFrame instead of polars. |
Returns
One row per input play with every text-derivable PBP_SCHEMA column populated (play_type, hit/out flags, rbi, pitch_sequence, runner movement, ...). Empty input returns a zero-row frame with the documented schema.
| col_name | type | description |
|---|---|---|
contest_id | String | |
inning | Int64 | |
inning_top_bot | String | |
batting | String | |
fielding | String | |
play_number | Int64 | |
score_away | Int64 | |
score_home | Int64 | |
batter | String | MLBAM player id of the batter. |
play_type | String | |
hit_trajectory | String | |
fielded_position | String | |
is_hit | Boolean | |
is_out | Boolean | |
strikeout_type | String | |
is_sacrifice | Boolean | |
sac_type | String | |
is_double_play | Boolean | |
rbi | Int64 | |
count_balls | Int64 | |
count_strikes | Int64 | |
pitch_sequence | String | |
error_position | String | |
unearned | Boolean | |
runs_scored | Int64 | |
scoring_runners | List | |
runners_advanced | List | |
outs_on_play | Int64 | |
is_scoring_play | Boolean | Flag indicating that the play put points on the board (1 = scoring play, 0 = not). |
description | String |
Example
from sportsdataverse.baseball.college_baseball import decompose_college_baseball_plays
df = decompose_college_baseball_plays(
[{"inning": 1, "inning_top_bot": "top", "score": "0-0",
"description": "Jack Moss singled to left field (1-2 KBFX)."}]
)
print(df.select("play_type", "is_hit", "pitch_sequence").row(0))