Skip to main content

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).

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

ParameterTypeDefaultDescription
seasonsUnion[int, List[int], None]NoneSee the core function.
stateOptional[DataFrame]NoneSee the core function.
return_as_pandasboolFalseReturn pandas.DataFrame instead of polars.

Returns

see the core function's Returns table.

col_nametypedescription
base_stateString3-char base occupancy code ("_" = empty, "1"/"2"/"3" = occupied), e.g. "1_3" for runners on first and third.
outsInt64Outs at the start of the base-out state (0-2).
run_expectancyFloat64Empirical mean runs scored from this state through the end of the half-inning (RE24).
nInt64Number 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

ParameterTypeDefaultDescription
playsDict[str, Any]Raw payload from espn_college_baseball_game_plays(event_id, return_parsed=False).

Returns

see the core function's Returns table.

col_nametypedescription
game_idString
inningInt64
halfString
base_stateString
outsInt64
runs_beforeInt64
runs_afterInt64
batting_team_idString
play_seqInt64
score_diffInt64

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

ParameterTypeDefaultDescription
seasonsUnion[int, List[int], None]NoneSee the core function.
stateOptional[DataFrame]NoneSee the core function.
resultsOptional[DataFrame]NoneSee the core function.
return_as_pandasboolFalseReturn pandas.DataFrame instead of polars.

Returns

see the core function's Returns table.

col_nametypedescription
game_idStringESPN event id for the game (join key to the schedule).
play_seqInt64Game-global sequential plate-appearance order.
re_beforeFloat64RE24 of the base-out state before the PA.
re_afterFloat64RE24 of the base-out state after the PA.
run_valueFloat64re_after minus re_before, plus runs scored on the play.
wpaFloat64Home-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

ParameterTypeDefaultDescription
rowslist[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_pandasboolFalseReturn 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_nametypedescription
contest_idString
inningInt64
inning_top_botString
battingString
fieldingString
play_numberInt64
score_awayInt64
score_homeInt64
batterStringMLBAM player id of the batter.
play_typeString
hit_trajectoryString
fielded_positionString
is_hitBoolean
is_outBoolean
strikeout_typeString
is_sacrificeBoolean
sac_typeString
is_double_playBoolean
rbiInt64
count_ballsInt64
count_strikesInt64
pitch_sequenceString
error_positionString
unearnedBoolean
runs_scoredInt64
scoring_runnersString
runners_advancedString
outs_on_playInt64
is_scoring_playBooleanFlag indicating that the play put points on the board (1 = scoring play, 0 = not).
descriptionString

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))