Skip to main content
Version: 0.2.0

MLB — additional Python functions — Analytics: tto_penalty–x_era

tto_penalty_table​

tto_penalty_table(feats: 'pl.DataFrame', *, return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Observed mean run value by times-through-order, with the penalty vs TTO=1.

Parameters

ParameterTypeDefaultDescription
featsDataFrameOutput of sportsdataverse.mlb.mlb_pitch_features.add_sequence_features (needs times_through_order and run_value).
return_as_pandasboolFalseWhen True, return a pandas.DataFrame.

Returns

times_through_order, mean_run_value, penalty_vs_first (mean_run_value minus the TTO=1 mean run value), n. Empty input returns a zero-row frame with this schema.

col_nametypedescription
times_through_orderInt64Times through the batting order (1-3).
mean_run_valueFloat64Mean observed run value for pitches at this TTO level.
penalty_vs_firstFloat64mean_run_value minus the TTO=1 mean run value.
nInt64Number of pitches observed at this TTO level.

Example

from sportsdataverse.mlb.mlb_pitch_features import pitch_features, add_sequence_features
from sportsdataverse.mlb.mlb_pitch_fatigue import tto_penalty_table
feats = add_sequence_features(pitch_features(raw_pitches))
out = tto_penalty_table(feats)
print(out.sort("times_through_order"))

x_era​

x_era(pitches: 'pl.DataFrame', season: 'int', *, return_as_pandas: 'bool' = False) -> "'Union[pl.DataFrame, pd.DataFrame]'"

Parametric xERA from Statcast expected wOBA allowed.

Parameters

ParameterTypeDefaultDescription
pitchesDataFrameRaw (or feature-substrate) pitch frame carrying pitcher and estimated_woba_using_speedangle.
seasonintSeason year (selects the league baseline via sportsdataverse.mlb.mlb_pitching_constants.get_baselines).
return_as_pandasboolFalseWhen True, return a pandas.DataFrame.

Returns

pitcher, season, x_woba, x_era. Empty input returns a zero-row frame with this schema.

col_nametypedescription
pitcherInt64Whether the position is a pitcher.
seasonInt64Season year.
x_wobaFloat64Expected wOBA allowed, the mean of Statcast estimated_woba_using_speedangle over the pitcher's pitches that carry one (batted balls).
x_eraFloat64Parametric xERA, league ERA + (x_woba - league wOBA) / wOBA scale * PA per 9, from the season's league baselines.

Example

from sportsdataverse.mlb.mlb_pitch_era import x_era
out = x_era(raw_pitches, 2024)
print(out.sort("x_era").head())