Skip to main content

NHL — additional Python functions — Models and calculators: ImpactConfig–nhl_xg

ImpactConfig​

ImpactConfig(goals_per_win: 'float', replacement_ev_off: 'float', replacement_ev_def: 'float', league_xg_rate_ev: 'float', league_xg_rate_pp: 'float', league_xg_rate_pk: 'float', rapm_lambda_grid: 'list[float]' = <factory>, penalty_goal_weight: 'float' = 0.18, faceoff_goal_weight: 'float' = 0.02, rink_x_goal_line: 'float' = 89.0, danger_high: 'dict' = <factory>, danger_medium: 'dict' = <factory>, xg_booster_league: 'str' = 'nhl') -> None

League-specific constants consumed by every player-impact engine function.

Parameters

ParameterTypeDefaultDescription
goals_per_winfloatgoals-per-win denominator for GAR->WAR (a seeded value until it is fitted from NHL team wins vs goal differential).
replacement_ev_offfloatEV xGF/60 of a unit of five replacement-level skaters (a TEAM on-ice rate, not a RAPM coefficient; see replacement_rapm).
replacement_ev_deffloatEV xGA/60 allowed by a unit of five replacement-level skaters.
league_xg_rate_evfloatleague-average even-strength xG rate (per 60), the level the two replacement rates are measured against.
league_xg_rate_ppfloatleague-average power-play xGF rate (per 60).
league_xg_rate_pkfloatleague-average penalty-kill xGA rate (per 60).
rapm_lambda_gridlist[float]<factory>candidate ridge penalties for the skater RAPM CV.
penalty_goal_weightfloat0.18goals-per-(penalty drawn - taken) conversion.
faceoff_goal_weightfloat0.02goals-per-(faceoff win - 0.5) conversion.
rink_x_goal_linefloat89.0absolute rink x-coordinate of the goal line (feet), used by the shot-geometry expansion.
danger_highdict<factory>{"max_distance": float, "max_angle": float} band for "high" danger.
danger_mediumdict<factory>same shape, wider band for "medium" danger; outside both -> "low".
xg_booster_leaguestr'nhl'which league's published boosters back this league's nhl_xg scoring (the PWHL borrows the NHL boosters -- a documented approximation).

Methods

ImpactConfig.replacement_rapm​

ImpactConfig.replacement_rapm() -> 'tuple[float, float]'

Replacement level on the skater-RAPM scale: (offense, defense) in xG/60.

The 5v5 fit nhl_skater_war uses (nhl_skater_rapm(strength_states=["5v5"])) gives each skater a coefficient relative to the design's intercept (about the league-average rate; home ice is its own column), additive over the five skaters on ice, with defense signed so that positive means suppression. The replacement rates are team on-ice levels, so each skater's share of the unit's gap to the league average is a fifth of it. For the NHL both rates are fitted from the replacement pool's own RAPM (tools/fit_nhl_replacement_level.py), so the pool as a whole earns zero EV GAR; the PWHL rates are seeds.

Returns

((replacement_ev_off - league_xg_rate_ev) / 5, (league_xg_rate_ev - replacement_ev_def) / 5); both negative for a below-average replacement unit.

Example

from sportsdataverse.nhl.nhl_player_impact_constants import get_constants
off, dfn = get_constants("nhl").replacement_rapm() # (-0.058, -0.072)

LeagueConstants​

LeagueConstants(hfa: 'float', margin_sd: 'float', avg_xgf: 'float', avg_total_goals: 'float', total_scale: 'float', shrink_k: 'float', prop_kappa: 'dict', pos_priors: 'dict', prop_team_volume_slope: 'float', in_game_wp_artifact: 'str', min_season: 'int', reg_draw_inflation: 'float', avg_shots_against: 'float', league_shots_k: 'float', shots_hfa: 'float', starter_shot_share: 'float', save_pct_prior: 'float', save_pct_kappa: 'float', shots_nb_size: 'float') -> None

Fitted, league-specific constants for the NHL/PWHL prediction spine.

Parameters

ParameterTypeDefaultDescription
hfafloathome-ice edge on exp_margin's own (shrunk-rating) scale. NHL: fitted jointly with margin_sd, so smaller than the real-goal home edge. PWHL: a seeded placeholder.
margin_sdfloatstandard deviation of the final goal margin (deliberately WIDE for hockey).
avg_xgffloatleague mean even-strength xG-for, per game.
avg_total_goalsfloatleague mean settled total goals per game (a shootout counts one goal), the level of every predicted total.
total_scalefloatmultiplier converting rating differential to total-goals deviation.
shrink_kfloatgames-played prior strength for rating shrinkage.
prop_kappadictempirical-Bayes shrinkage strength per player-prop stat family.
pos_priorsdictper-position (F/D) per-stat-family prior rates.
prop_team_volume_slopefloatgame-script tilt on a player-prop projection (favored team -> fewer late shots-for). SEEDED PLACEHOLDER (~0.04), not yet fitted -- a future prop-fit task should estimate it from the realized shots-vs-exp_margin slope, mirroring how fit_props.py fits prop_kappa/pos_priors.
in_game_wp_artifactstrfilename of the bundled in-game win-probability model under sportsdataverse/nhl/models/.
min_seasonintearliest season this league's prediction spine supports.
reg_draw_inflationfloatmultiplier minus one on every tied cell of the regulation score grid (~sportsdataverse.nhl.nhl_market.regulation_goal_means). Independent Poissons under-predict 60-minute ties.
avg_shots_againstfloatseed for the league's in-season shots-on-goal per team-game, before any game of the season is played (~sportsdataverse.nhl.nhl_player_props.nhl_goalie_saves).
league_shots_kfloatteam-games of weight on avg_shots_against in the in-season league mean. SEEDED (two games a team), not fitted.
shots_hfafloathow many more shots on goal the home team takes than the away team.
starter_shot_sharefloatthe starting goalie's share of his team's expected shots against (he is sometimes relieved).
save_pct_priorfloatleague save percentage a goalie's own record is shrunk toward.
save_pct_kappafloatshots of weight on save_pct_prior in a goalie's shrunk save percentage.
shots_nb_sizefloatnegative-binomial size of a starter's shots against; his saves then follow a negative binomial with the same size (binomial thinning).

add_shot_geometry​

add_shot_geometry(df: 'pl.DataFrame', *, league: 'str' = 'nhl') -> 'pl.DataFrame'

Attach distance_to_net / shot_angle / shot_danger (descriptive output only).

Distance/angle are computed off x_fixed/y against the rink goal-line x-coordinate in LEAGUE_CONSTANTS[league].rink_x_goal_line; shot_danger buckets into high/medium/low using the danger_high/danger_medium distance+angle bands from the same config. These are output columns only -- never fed back into the boosters (Decision D2; a new feature would force a retrain).

Parameters

ParameterTypeDefaultDescription
dfDataFrameany frame carrying x_fixed and y columns.
leaguestr'nhl'"nhl" or "pwhl" -- selects the danger-zone bands.

Returns

df with distance_to_net:Float64, shot_angle:Float64, shot_danger:Utf8 appended.

col_nametypedescription
event_typeStringStandardized event type code.
eventStringEvent description label.
secondary_typeStringSecondary event type (e.g. shot type).
event_team_abbrStringAbbreviation of the team credited with the event.
event_team_typeStringWhether the event team is home or away.
descriptionStringFull text description of the event.
periodInt64Period number.
period_typeStringPeriod type (REG/OT/SO).
period_timeStringElapsed time in the period (MM:SS).
period_secondsInt64Elapsed seconds in the period.
period_seconds_remainingInt64Seconds remaining in the period.
period_time_remainingStringTime remaining in the period (MM:SS).
game_secondsInt64Elapsed seconds in the game.
game_seconds_remainingInt64Seconds remaining in regulation.
home_scoreInt64Home team final score.
away_scoreInt64Away team final score.
event_player_1_nameStringName of the primary event player.
event_player_1_typeStringRole of the primary event player.
event_player_1_idInt64Player id of the primary event player.
event_player_2_nameStringName of the secondary event player.
event_player_2_typeStringRole of the secondary event player.
event_player_2_idInt64Player id of the secondary event player.
event_player_3_nameStringName of the tertiary event player.
event_player_3_typeStringRole of the tertiary event player.
event_player_3_idInt64Player ID of the tertiary event player.
event_goalie_nameStringName of the goalie on the event.
event_goalie_idInt64Player id of the goalie on the event.
penalty_severityStringSeverity of the penalty.
penalty_minutesInt64Penalty minutes.
strength_stateStringStrength state (e.g. 5v5, 5v4).
strength_codeStringStrength state code (e.g., all, even, pp, pk).
strengthStringStrength label (Even, Power Play, Shorthanded).
empty_netBooleanWhether the net was empty.
extra_attackerBooleanWhether an extra attacker was on the ice.
xInt64Raw x-coordinate of the event.
yInt64Raw y-coordinate of the event.
x_fixedInt64Normalized x coordinate (home shoots right).
y_fixedInt64Normalized y coordinate (home shoots right).
shot_distanceFloat64Distance of the shot from the net.
shot_angleFloat64Angle of the shot relative to the net.
home_skatersInt64Number of home skaters on the ice.
away_skatersInt64Number of away skaters on the ice.
home_on_1StringName of home skater 1 on the ice.
home_on_2StringName of home skater 2 on the ice.
home_on_3StringName of home skater 3 on the ice.
home_on_4StringName of home skater 4 on the ice.
home_on_5StringName of home skater 5 on the ice.
home_on_6StringName of home skater 6 on the ice.
home_on_7StringName of home skater 7 on the ice.
away_on_1StringName of away skater 1 on the ice.
away_on_2StringName of away skater 2 on the ice.
away_on_3StringName of away skater 3 on the ice.
away_on_4StringName of away skater 4 on the ice.
away_on_5StringName of away skater 5 on the ice.
away_on_6StringName of away skater 6 on the ice.
away_on_7StringName of away skater 7 on the ice.
home_goalieStringName of the home goalie on the ice.
away_goalieStringName of the away goalie on the ice.
num_onInt64Number of players coming on (line change).
players_onStringNames of players coming on.
num_offInt64Number of players going off (line change).
players_offStringNames of players going off.
game_idInt64Unique game identifier.
seasonStringSeason year (echoed from arg).
season_typeStringSeason type code (echoed from arg).
home_abbrStringHome team abbreviation.
away_abbrStringAway team abbreviation.
event_idxInt64Sequential event index within the game.
event_idInt64ESPN event id (echoed from arg).
pptReplayUrlStringURL to the play replay, if available.
away_goalie_inInt64Whether the away goalie is on the ice (1/0).
home_goalie_inInt64Whether the home goalie is on the ice (1/0).
reasonStringReason for the event (e.g. stoppage reason).
secondaryReasonStringSecondary reason for a stoppage.
ids_onStringPlayer ids coming on.
ids_offStringPlayer ids going off.
home_on_1_idInt64Player id of home skater 1 on the ice.
away_on_1_idInt64Player id of away skater 1 on the ice.
home_on_2_idInt64Player id of home skater 2 on the ice.
away_on_2_idInt64Player id of away skater 2 on the ice.
home_on_3_idInt64Player id of home skater 3 on the ice.
away_on_3_idInt64Player id of away skater 3 on the ice.
home_on_4_idInt64Player id of home skater 4 on the ice.
away_on_4_idInt64Player id of away skater 4 on the ice.
home_on_5_idInt64Player id of home skater 5 on the ice.
away_on_5_idInt64Player id of away skater 5 on the ice.
home_on_6_idInt64Player id of home skater 6 on the ice.
away_on_6_idInt64Player id of away skater 6 on the ice.
home_on_7_idInt64Player id of home skater 7 on the ice.
away_on_7_idInt64Player id of away skater 7 on the ice.
home_goalie_idInt64Player ID of the home goalie on the ice.
away_goalie_idInt64Player ID of the away goalie on the ice.
xgFloat64Expected goals value for the shot event.
game_dateStringGame date.
distance_to_netFloat64
shot_dangerString

Example

import polars as pl
from sportsdataverse.nhl.nhl_xg import add_shot_geometry
out = add_shot_geometry(pl.DataFrame({"x_fixed": [80], "y": [0]}))

adjust_rate_opponent​

adjust_rate_opponent(game_rates: 'pl.DataFrame', *, for_col: 'str', against_col: 'str', hfa: 'float', avg: 'float', shrink_k: 'float', max_iter: 'int' = 100, tol: 'float' = 0.0001) -> 'pl.DataFrame'

Opponent-adjust a per-game for/against rate by iterative fixed-point, then shrink.

League-agnostic: every constant (hfa, avg, shrink_k) is passed in -- no NHL/PWHL number is hard-coded here. This is the flagged T7.2 "rate-iterative + shrinkage" shared-solver candidate (the hockey counterpart of the NFL/CFB per-play ridge); for_col/against_col are symmetric (offense sees opponent defense).

Parameters

ParameterTypeDefaultDescription
game_ratesDataFrameone row per (team, opponent, game) with columns season, team, opp_team, is_home, neutral_site, and the two numeric rate columns named by for_col/against_col.
for_colstrname of the team's own-side rate column (e.g. "xgf").
against_colstrname of the team's against-side rate column (e.g. "xga").
hfafloathome-ice edge added to the home side / subtracted from the away side.
avgfloatleague mean rate to adjust and shrink toward.
shrink_kfloatgames-played prior strength for the post-convergence shrink.
max_iterint100maximum fixed-point iterations.
tolfloat0.0001convergence tolerance on the max absolute update.

Returns

A polars DataFrame, one row per (season, team). |col_name |type | |:------------|:------| |season |Int64 | |team |String | |adj_for |Float64| |adj_against |Float64| |adj_net |Float64| |raw_for |Float64| |raw_against |Float64| |games |Int64 |

No returns table is published for this function: no capture: it needs a schedule with date / home_abbr / away_abbr / neutral_site, and no loader returns one (load_nhl_schedules carries game_date / home_team_abbr).

Example

from sportsdataverse.nhl.nhl_team_ratings import adjust_rate_opponent
adjust_rate_opponent(
game_rates, for_col="xgf", against_col="xga",
hfa=0.2, avg=2.55, shrink_k=15.0,
)

as_of_ratings_split​

as_of_ratings_split(df: 'pl.DataFrame', cutoff_date: '_dt.date', *, date_col: 'str' = 'date') -> 'pl.DataFrame'

Filter a frame to rows strictly before cutoff_date (the leakage boundary).

Parameters

ParameterTypeDefaultDescription
dfDataFramea polars DataFrame with a date column.
cutoff_datedatethe game date being predicted; only strictly-earlier rows are kept.
date_colstr'date'name of the date column (default "date").

Returns

The subset of df with df[date_col] < cutoff_date.

col_nametypedescription
game_idInt64Unique game identifier.
season_fullStringFull season label (e.g. 20212022).
game_typeStringGame type the row belongs to.
game_dateStringGame date.
game_timeStringScheduled start time of the game.
home_team_abbrStringHome team abbreviation.
away_team_abbrStringAway team abbreviation.
home_team_nameStringHome team name.
away_team_nameStringAway team name.
home_scoreInt64Home team final score.
away_scoreInt64Away team final score.
game_stateStringGame state (e.g., FINAL, LIVE).
venueStringVenue where the game was played.
series_letterString
playoff_roundInt64Playoff round identifier.
series_game_numberInt64
seasonInt64Season year (echoed from arg).
game_jsonBooleanWhether processed game JSON is available.
game_json_urlStringURL to the processed game JSON.
PBPBooleanWhether play-by-play data is available.
team_boxBooleanWhether team box score data is available.
player_boxBooleanWhether player box score data is available.
skater_boxBooleanWhether skater box data is available.
goalie_boxBooleanWhether goalie box data is available.
game_infoBooleanWhether game info data is available.
game_rostersBooleanWhether game rosters data is available.
scoringBoolean
penaltiesBooleanPenalty count.
scratchesBoolean
linescoreBoolean
three_starsBooleanWhether three stars data is available.
shiftsBooleanNumber of shifts.
officialsBooleanWhether officials data is available.
shots_by_periodBooleanWhether shots-by-period data is available.
shootoutBooleanWhether shootout data is available.

Example

import datetime as dt
import polars as pl
from sportsdataverse.nhl.nhl_prediction_constants import as_of_ratings_split
df = pl.DataFrame({"date": [dt.date(2023, 1, 1), dt.date(2023, 1, 2)]})
as_of_ratings_split(df, dt.date(2023, 1, 2))

booster_cache_dir​

booster_cache_dir(override: 'str | Path | None' = None) -> 'Path'

Resolve the local cache directory for the downloaded nhl_xg_models boosters.

Precedence: explicit override argument > NHL_XG_MODEL_DIR env var > ~/.cache/nhl_xg_models.

Parameters

ParameterTypeDefaultDescription
overridestr | Path | NoneNonean explicit directory (e.g. a committed test-fixture dir); wins over the env var when given.

Returns

The resolved pathlib.Path (not created here -- ensure_xg_models creates it on first download).

Example

from sportsdataverse.nhl.nhl_player_impact_constants import booster_cache_dir
d = booster_cache_dir()

brier_score​

brier_score(y_true: 'np.ndarray', p_pred: 'np.ndarray') -> 'float'

Mean squared error between predicted probabilities and binary outcomes.

Parameters

ParameterTypeDefaultDescription
y_truendarrayArray of binary outcomes (0/1).
p_predndarrayArray of predicted probabilities in [0, 1].

Returns

The Brier score (0.0 is a perfect forecast).

Example

import numpy as np
from sportsdataverse._common.metrics import brier_score
brier_score(np.array([1, 0]), np.array([0.9, 0.1]))

build_design​

build_design(stints: 'pl.DataFrame') -> "tuple['sp.csr_matrix', np.ndarray, np.ndarray, list[int]]"

Build the sparse RAPM design matrix -- two rows per stint (one per attacking team).

Parameters

ParameterTypeDefaultDescription
stintsDataFramea build_stints-shaped frame.

Returns

(X, y, w, player_index) where X is a scipy.sparse.csr_matrix with columns off_<player> (all on-ice attackers), def_<player> (all on-ice defenders), then a trailing home-ice indicator and intercept column; y is the attacking team's xGF per 60; w is stint duration (seconds); player_index maps each off_/def_ column pair's position to a player_id (so column j is off_<player_index[j]> and column j + n_players is def_<player_index[j]>).

Example

from sportsdataverse.nhl.nhl_rapm import build_design
X, y, w, player_index = build_design(stints)

build_stints​

build_stints(shifts: 'pl.DataFrame', scored: 'pl.DataFrame', *, as_of: 'int | None' = None) -> 'pl.DataFrame'

Fold load_nhl_shifts CHANGE events into contiguous constant-personnel intervals.

Per game: resolves each shift row's full team name (event_team) to home/away via team_fullname_to_abbr + the game's home_abbr/away_abbr (from scored), then folds ids_on/ids_off deltas chronologically into a running on-ice set per side. A new interval begins at every distinct game_seconds boundary; the final interval is closed at the last scored event's game_seconds + 1 for that game (there is no explicit "end of game" CHANGE row in the shift-chart feed). A shot or goal at a change second credits its xG to the interval that ends there (the players on the ice before the change), following fastRhockey's within-second event order (R/nhl_game_feed.R: shots, goals, stops and penalties before a CHANGE, faceoffs after). So xG sums over (start_s, end_s] while goalie and strength_state read the events in [start_s, end_s), falling back to the xG window when that is empty. An interval no event falls in (typically a line-change transient) keeps the goalies and strength of the latest event before it (the game's first event, for an opening one), unless a goalie the game's events name came on or went off at its start (then both stay null until the next event), and its strength stays null when its skater counts match neither the label nor the interval that set it (a penalty that expired with no event).

Shift-chart id lists do not distinguish position: home_goalie/away_goalie come from the overlapping scored events' home_goalie_id/away_goalie_id (the modal value in the interval, or the carried one) and are removed from home_ids/away_ids. A null goalie leaves the id list as it is.

Parameters

ParameterTypeDefaultDescription
shiftsDataFramea load_nhl_shifts-shaped frame. A team_abbr column, when present, names each row's team directly (the PWHL adapter supplies it).
scoredDataFramean nhl_xg-scored frame (for the game's home_abbr/away_abbr and each interval's on-ice xG-for and goalie).
as_ofint | NoneNonean optional per-game game_seconds cutoff -- intervals starting at or after as_of are dropped and one that straddles it ends at as_of. This is the leakage boundary for any forward-looking use: features for a game/date must use only stints strictly before that game's cutoff.

Returns

one row per interval -- game_id:Int64, period:Int64, start_s:Int64, end_s:Int64, duration:Int64, home_ids:List(Int64), away_ids:List(Int64), home_goalie:Int64, away_goalie:Int64, strength_state:Utf8, xgf_home:Float64, xgf_away:Float64. Empty/malformed shifts returns a zero-row frame with this schema.

No returns table is published for this function: no capture: it runs longer than the capture allows even on 53 games (build_stints makes one eager filter per shift change).

Example

import polars as pl
from sportsdataverse.nhl.nhl_xg import nhl_xg
from sportsdataverse.nhl.nhl_rapm import build_stints
pbp = pl.read_parquet("tests/fixtures/nhl_player_impact/pbp_sample.parquet")
shifts = pl.read_parquet("tests/fixtures/nhl_player_impact/shifts_sample.parquet")
scored = nhl_xg(pbp, model_dir="tests/fixtures/nhl_player_impact/xg_models")
stints = build_stints(shifts, scored)

calibration_table​

calibration_table(y_true: 'np.ndarray', p_pred: 'np.ndarray', n_bins: 'int' = 10) -> 'pl.DataFrame'

Bucket predicted probabilities into bins and compare to actual outcome rates.

Parameters

ParameterTypeDefaultDescription
y_truendarrayArray of binary outcomes (0/1).
p_predndarrayArray of predicted probabilities in [0, 1].
n_binsint10Number of equal-width probability bins.

Returns

A polars.DataFrame with columns bin_mid, mean_pred, mean_actual, n (one row per non-empty bin).

col_nametypedescription
bin_midFloat64Midpoint of the equal-width predicted-probability bin (0.05, 0.15, ... with the default 10 bins).
mean_predFloat64Mean predicted probability of the rows in the bin.
mean_actualFloat64Observed outcome rate of the rows in the bin; a calibrated model has it close to mean_pred.
nInt64Rows in the bin. Empty bins emit no row.

Example

import numpy as np
from sportsdataverse._common.metrics import calibration_table
calibration_table(np.array([1, 0, 1, 0]), np.array([0.9, 0.1, 0.8, 0.2]))

ensure_xg_models​

ensure_xg_models(model_dir: 'str | Path | None' = None) -> 'Path'

Return a dir holding the published boosters, checked against the release manifest.

Mirrors the fastRhockey/nflverse download-on-demand + cache pattern -- the documented exception to "no first-use download" (the boosters are a large, already-published, already-validated artifact; see Decision D1 in the design spec). Each call fetches the release's xg_model_meta.json manifest and re-downloads any cached booster whose sha256 differs from it, so a retrained release replaces an old cache once the new manifest is fetched. A refresh is staged: every replacement booster is downloaded and verified before any file is written, and the manifest is written last, so a refresh that fails part-way leaves the previous manifest and boosters intact. Offline, the cached manifest is used. An explicit model_dir that already holds both boosters (e.g. the committed offline test fixtures) is used as-is and never touches the network.

Parameters

ParameterTypeDefaultDescription
model_dirstr | Path | NoneNonedirectory of local models; None resolves the cache via booster_cache_dir() (env NHL_XG_MODEL_DIR override, else ~/.cache/nhl_xg_models). An explicit dir missing a booster is filled from the release like the cache.

Returns

The resolved directory containing xg_model_5v5.json, xg_model_st.json and (except for local models that ship without one) xg_model_meta.json.

Raises

ExceptionDescription
AssetFetchErrorThe manifest could not be fetched and no copy is cached, or a downloaded booster does not match the manifest (the release may be mid-update; try again shortly).
ValueErrorNeither the fetched nor the cached manifest pins both boosters.
requests.RequestExceptionA booster that is missing or stale in the cache could not be downloaded (after the retries of download).
NoDataErrorA missing or stale booster is not on the release (404).

Example

from sportsdataverse.nhl.nhl_xg import ensure_xg_models
d = ensure_xg_models() # downloads on first use, re-checked every load

get_constants​

get_constants(league: 'str') -> 'LeagueConstants'

Resolve the fitted-constants row for a league.

Parameters

ParameterTypeDefaultDescription
leaguestr"nhl" or "pwhl".

Returns

The LeagueConstants row for league.

Raises

ExceptionDescription
ValueErrorIf league is not a known key of LEAGUE_CONSTANTS.

Example

from sportsdataverse.nhl.nhl_prediction_constants import get_constants
get_constants("nhl").margin_sd

load_xg_models​

load_xg_models(model_dir: 'str | Path | None' = None) -> 'dict'

Load the two published boosters (+ embedded feature names) and the penalty-shot constant.

Parameters

ParameterTypeDefaultDescription
model_dirstr | Path | NoneNoneNone uses the canonical nhl_xg_models release, cached under booster_cache_dir() and checked against the release manifest on every load (see ensure_xg_models); pass a dir to use local models (the offline test suite always passes the committed fixture dir).

Returns

dict with keys m5v5/mst (xgboost.Booster), feats_5v5/feats_st (embedded feature-name lists), and ps (penalty-shot constant probability).

Example

from sportsdataverse.nhl.nhl_xg import load_xg_models
models = load_xg_models("tests/fixtures/nhl_player_impact/xg_models")

log_loss_score​

log_loss_score(y_true: 'np.ndarray', p_pred: 'np.ndarray', eps: 'float' = 1e-15) -> 'float'

Binary cross-entropy loss between predicted probabilities and outcomes.

Parameters

ParameterTypeDefaultDescription
y_truendarrayArray of binary outcomes (0/1).
p_predndarrayArray of predicted probabilities in [0, 1].
epsfloat1e-15Clipping bound to avoid log(0).

Returns

The mean log loss.

Example

import numpy as np
from sportsdataverse._common.metrics import log_loss_score
log_loss_score(np.array([1, 0]), np.array([0.9, 0.1]))

mae​

mae(a: 'np.ndarray', b: 'np.ndarray') -> 'float'

Mean absolute error between two arrays.

Parameters

ParameterTypeDefaultDescription
andarrayFirst array of values.
bndarraySecond array of values (same length as a).

Returns

The mean absolute error.

Example

import numpy as np
from sportsdataverse._common.metrics import mae
mae(np.array([1.0, 2.0]), np.array([1.5, 2.5]))

nhl_expected_assists​

nhl_expected_assists(pbp: 'pl.DataFrame', *, league: 'str' = 'nhl', xg_model: 'ShotXGModel | None' = None, return_as_pandas: 'bool' = False) -> 'pl.DataFrame | pd.DataFrame'

Per-player expected primary/secondary assists from xG-weighted goal credit.

Each goal credits its assist1 player its relative danger goal_xg / mean_goal_xg as x_primary (and assist2 likewise as x_secondary). Normalizing to the league-mean goal xG is what makes the total credit unbiased -- Sum(x_primary + x_secondary) ~= Sum(actual assists) -- while still rewarding a playmaker who sets up high-danger goals (relative danger > 1) over one who feeds tap-ins (< 1). Crediting raw goal_xg (~0.1-0.2) instead would put expected assists on the xG scale, an order of magnitude below the assist count, and could never be unbiased against actual assists. assists_above_expected = (primary + secondary) - (x_primary + x_secondary) (positive = the player's assisted goals were lower-danger than average, so they out-assisted their shot quality); primary_share = primary / (primary + secondary).

Parameters

ParameterTypeDefaultDescription
pbpDataFrameParsed pbp frame (Task-0.1 contract).
leaguestr'nhl'League key (unused today -- assist credit is league-agnostic; kept for signature parity with the other microstat models and the PWHL shim).
xg_modelShotXGModel | NoneNoneA fitted ~sportsdataverse.nhl.nhl_microstat_constants.ShotXGModel; fit on pbp when None.
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

Per-player frame: player_id, primary_assists, secondary_assists, x_primary_assists, x_secondary_assists, assists_above_expected, primary_share. Zero-row input returns a zero-row frame with this schema.

col_nametypedescription
player_idStringUnique player identifier.
primary_assistsInt64
secondary_assistsInt64
x_primary_assistsFloat64
x_secondary_assistsFloat64
assists_above_expectedFloat64
primary_shareFloat64

Example

from sportsdataverse.nhl.nhl_expected_assists import nhl_expected_assists

out = nhl_expected_assists(pbp)

# PWHL

out_pwhl = nhl_expected_assists(pwhl_pbp, league="pwhl")

nhl_goalie_gsax​

nhl_goalie_gsax(pbp: 'pl.DataFrame', shifts: 'pl.DataFrame', *, model_dir: "'str | None'" = None, league: 'str' = 'nhl', xg_scored: 'bool' = False, return_as_pandas: 'bool' = False) -> "'pl.DataFrame | pd.DataFrame'"

Per-goalie goals-saved-above-expected (GSAx) for the games in pbp.

Scores every unblocked shot via nhl_xg, attributes each shot to the defending goalie (_attribute_goalie), and aggregates xga = sum(xg), ga = count(goals), gsax = xga - ga. gsax_per_60 uses an on-ice-seconds proxy derived from the pbp event span each goalie is credited on (see _toi_seconds_by_goalie) -- shifts is accepted for interface parity with the rest of the player-impact spine but is not currently required for TOI.

Parameters

ParameterTypeDefaultDescription
pbpDataFramea load_nhl_pbp_full-shaped frame (or an already nhl_xg-scored one -- re-scoring is idempotent since the prior xg column is dropped first).
shiftsDataFramea load_nhl_shifts-shaped frame (currently unused; accepted for interface parity -- see the module docstring).
model_dirstr | NoneNonepassed through to nhl_xg (booster directory).
leaguestr'nhl'"nhl" or "pwhl".
xg_scoredboolFalsepbp already carries the shot xg to use (the PWHL adapter scores HockeyTech shots with its own model), so skip the nhl_xg re-score.
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.

Returns

player_id:Int64, goalie:Utf8, shots:Int64, xga:Float64, ga:Int64, gsax:Float64, gsax_per_60:Float64. League-wide sum(gsax) == sum(xga) - sum(goals), which is ~= 0 at large sample and exactly zero only under perfect league-wide xG calibration. Empty/malformed input returns a zero-row frame with this schema -- never raises.

col_nametypedescription
player_idInt64Goalie's player id.
goalieStringFull name of the goalie the shots were credited to (the defending side's goalie on each shot).
shotsInt64Unblocked shots faced (on-net shots for PWHL input), excluding empty-net shots.
xgaFloat64Expected goals against, the sum of xg over the shots faced.
gaInt64Goals allowed on those shots.
gsaxFloat64Goals saved above expected, xga minus ga.
gsax_per_60Float64gsax per 60 minutes of the event-span time-on-ice proxy.

Example

import polars as pl
from sportsdataverse.nhl.nhl_gsax import nhl_goalie_gsax
pbp = pl.read_parquet("tests/fixtures/nhl_player_impact/pbp_sample.parquet")
gsax = nhl_goalie_gsax(pbp, pl.DataFrame(), model_dir="tests/fixtures/nhl_player_impact/xg_models")
print(gsax.sort("gsax", descending=True))

# Pipeline next step

gsax.filter(pl.col("shots") >= 10).sort("gsax_per_60", descending=True).head()

nhl_skater_rapm​

nhl_skater_rapm(pbp: 'pl.DataFrame', shifts: 'pl.DataFrame', *, model_dir: "'str | None'" = None, league: 'str' = 'nhl', lam: 'float | None' = None, as_of: 'int | None' = None, strength_states: 'list[str] | None' = None, return_as_pandas: 'bool' = False, xg_scored: 'bool' = False, _stints: 'pl.DataFrame | None' = None) -> "'pl.DataFrame | pd.DataFrame'"

Per-skater xG-based Regularized Adjusted Plus-Minus (RAPM), per 60 minutes.

Builds shift stints (build_stints), the sparse off/def design matrix (build_design), and solves the weighted ridge (weighted_ridge). Offensive rating is the off_<player> coefficient; defensive rating is the negated def_<player> coefficient (suppressing xG-against is positive value) -- xg_rapm = xg_rapm_off + xg_rapm_def.

Parameters

ParameterTypeDefaultDescription
pbpDataFramea load_nhl_pbp_full-shaped frame.
shiftsDataFramea load_nhl_shifts-shaped frame.
model_dirstr | NoneNonepassed through to nhl_xg.
leaguestr'nhl'"nhl" or "pwhl" -- selects the ridge lambda-grid via LEAGUE_CONSTANTS when lam is not given.
lamfloat | NoneNonean explicit ridge penalty; None selects via k-fold CV over LEAGUE_CONSTANTS[league].rapm_lambda_grid.
as_ofint | NoneNoneforwarded to build_stints -- the leakage-boundary cutoff.
strength_stateslist[str] | NoneNonerestrict the design matrix to these strength_state values (e.g. ["5v5"] for an even-strength-only fit, as used by nhl_skater_war's ev_off/ev_def components so they don't overlap with nhl_special_teams_value's PP/PK components). None (default) uses every strength state, matching the general-purpose all-situations RAPM.
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.
xg_scoredboolFalsepbp already carries the shot xg to use (another league's own xG model); skip re-scoring with the NHL boosters, which would replace it.
_stintsDataFrame | NoneNoneinternal test hook -- inject a pre-built stints frame, bypassing pbp/shifts/scoring (not part of the public contract).

Returns

player_id:Int64, xg_rapm_off:Float64, xg_rapm_def:Float64, xg_rapm:Float64, toi_minutes:Float64. Empty input returns a zero-row frame with this schema.

No returns table is published for this function: no capture: it runs longer than the capture allows even on 53 games (build_stints makes one eager filter per shift change).

Example

import polars as pl
from sportsdataverse.nhl.nhl_rapm import nhl_skater_rapm
pbp = pl.read_parquet("tests/fixtures/nhl_player_impact/pbp_sample.parquet")
shifts = pl.read_parquet("tests/fixtures/nhl_player_impact/shifts_sample.parquet")
rapm = nhl_skater_rapm(pbp, shifts, model_dir="tests/fixtures/nhl_player_impact/xg_models")
print(rapm.sort("xg_rapm", descending=True).head(10))

nhl_skater_war​

nhl_skater_war(pbp: 'pl.DataFrame', shifts: 'pl.DataFrame', *, model_dir: "'str | None'" = None, league: 'str' = 'nhl', return_as_pandas: 'bool' = False, xg_scored: 'bool' = False) -> "'pl.DataFrame | pd.DataFrame'"

Per-skater GAR/WAR composite -- EV + special-teams + faceoffs + penalties.

Parameters

ParameterTypeDefaultDescription
pbpDataFramea load_nhl_pbp_full-shaped frame.
shiftsDataFramea load_nhl_shifts-shaped frame.
model_dirstr | NoneNonepassed through to nhl_xg/nhl_skater_rapm/ nhl_special_teams_value.
leaguestr'nhl'"nhl" or "pwhl".
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.
xg_scoredboolFalsepbp already carries the shot xg to use (another league's own xG model); skip re-scoring with the NHL boosters, which would replace it.

Returns

player_id:Int64, ev_off:Float64, ev_def:Float64, pp:Float64, pk:Float64, pens:Float64, faceoffs:Float64, gar:Float64, war:Float64. ev_off/ev_def are (5v5-only RAPM rate - replacement level) * EV TOI/60, the replacement level on the RAPM scale (ImpactConfig.replacement_rapm), so a league-average skater earns positive EV GAR; gar sums every component; war = gar / goals_per_win. Empty input returns a zero-row frame with this schema.

No returns table is published for this function: no capture: it runs longer than the capture allows even on 53 games (build_stints makes one eager filter per shift change).

Example

import polars as pl
from sportsdataverse.nhl.nhl_war import nhl_skater_war
pbp = pl.read_parquet("tests/fixtures/nhl_player_impact/pbp_sample.parquet")
shifts = pl.read_parquet("tests/fixtures/nhl_player_impact/shifts_sample.parquet")
war = nhl_skater_war(pbp, shifts, model_dir="tests/fixtures/nhl_player_impact/xg_models")
print(war.sort("war", descending=True).head(10))

nhl_team_ratings​

nhl_team_ratings(seasons: 'Union[int, list[int]]', *, league: 'str' = 'nhl', as_of_date: '_dt.date | None' = None, pbp: 'Union[pl.DataFrame, pd.DataFrame, None]' = None, schedule: 'Union[pl.DataFrame, pd.DataFrame, None]' = None, return_as_pandas: 'bool' = False) -> 'Union[pl.DataFrame, pd.DataFrame]'

Opponent-adjusted, shrunk even-strength xG (+ goal) team ratings.

Loads pbp + schedule for seasons (or takes them preloaded), restricts to even strength, applies the as-of-date leakage split if requested, opponent-adjusts + shrinks both the xG rate (primary) and the realized-goal rate (concurrent sanity rating) via adjust_rate_opponent, and derives off/def/net ranks.

Parameters

ParameterTypeDefaultDescription
seasonsUnion[int, list[int]]an int or iterable of seasons (end year, e.g. 2024 for 2023-24). Only played regular-season games of these seasons are rated (a scheduled game with no pbp, not yet played or postponed, is left out). Several seasons are pooled into one solve and every row is labelled with the first season; call once per season, or use nhl_team_ratings_history, for per-season ratings.
leaguestr'nhl'"nhl" or "pwhl" -- resolves HFA/avg/shrink_k via sportsdataverse.nhl.nhl_prediction_constants.get_constants.
as_of_datedate | NoneNoneif given, only games strictly before this date are used (the leakage boundary for a predictive backtest).
pbpUnion[DataFrame, DataFrame, None]Nonea preloaded load_nhl_pbp_full(seasons) frame; None loads it. Passing it skips the download, so many as-of calls can share one load.
scheduleUnion[DataFrame, DataFrame, None]Nonea preloaded load_nhl_schedules(seasons) frame; None loads it.
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.

Returns

A polars (or pandas) DataFrame, one row per (season, team). Empty input seasons return a zero-row frame with the documented schema. |col_name |type | |:----------|:------| |season |Int64 | |team |String | |adj_xgf |Float64| |adj_xga |Float64| |adj_xg_net |Float64| |adj_gf |Float64| |adj_ga |Float64| |games |Int64 | |off_rank |Int64 | |def_rank |Int64 | |net_rank |Int64 | |net_z |Float64|

col_nametypedescription
seasonInt64Season year (echoed from arg).
teamStringTeam name.
adj_xgfFloat64
adj_xgaFloat64
adj_xg_netFloat64
adj_gfFloat64
adj_gaFloat64
gamesInt64Games played.
off_rankInt64
def_rankInt64
net_rankInt64
net_zFloat64

Raises

ExceptionDescription
ValueErrorleague has no constants.

Example

import polars as pl
from sportsdataverse.nhl.nhl_team_ratings import nhl_team_ratings

ratings = nhl_team_ratings(2023)
print(ratings.sort("net_rank").head())

# As-of-date leakage-safe rating

import datetime as dt
ratings = nhl_team_ratings(2023, as_of_date=dt.date(2023, 1, 1))

# Several as-of dates from one download

from sportsdataverse.nhl import load_nhl_pbp_full, load_nhl_schedules
pbp, sched = load_nhl_pbp_full([2023]), load_nhl_schedules([2023])
jan = nhl_team_ratings(2023, as_of_date=dt.date(2023, 1, 1), pbp=pbp, schedule=sched)
feb = nhl_team_ratings(2023, as_of_date=dt.date(2023, 2, 1), pbp=pbp, schedule=sched)

# Pipeline next step (one line)

ratings.filter(pl.col("team") == "TOR")

nhl_team_ratings_history​

nhl_team_ratings_history(seasons: 'Union[int, list[int]]', *, league: 'str' = 'nhl', as_of_dates: 'Iterable[_dt.date] | None' = None, pbp: 'Union[pl.DataFrame, pd.DataFrame, None]' = None, schedule: 'Union[pl.DataFrame, pd.DataFrame, None]' = None, return_as_pandas: 'bool' = False) -> 'Union[pl.DataFrame, pd.DataFrame]'

The as-of team ratings for every game date of a season, from one load.

For each season and each as-of date d, the rows are exactly nhl_team_ratings(season, as_of_date=d) with an as_of_date column added: the ratings each team carried into that day, built only from regular-season games strictly before d. The pbp and schedule are loaded once per season (or taken preloaded) and the per-game rates are built once, so a season costs one download instead of one per date. Each season is rated on its own, as a per-season nhl_team_ratings(season, ...) call is.

Parameters

ParameterTypeDefaultDescription
seasonsUnion[int, list[int]]an int or iterable of seasons (end year, e.g. 2024 for 2023-24).
leaguestr'nhl'"nhl" or "pwhl" -- resolves the rating constants, as in nhl_team_ratings.
as_of_datesIterable[date] | NoneNonethe dates to snapshot. None uses every scheduled regular-season game date through the first one after the season's last played game (so an in-progress season gets the snapshot for its next game day, and no repeats beyond it). Each date is applied to every season, as nhl_team_ratings(season, as_of_date=d) would be. A date with no earlier game in a season (such as the season's first game date) yields no rows for that season.
pbpUnion[DataFrame, DataFrame, None]Nonea preloaded load_nhl_pbp_full(seasons) frame; None loads each season's pbp once.
scheduleUnion[DataFrame, DataFrame, None]Nonea preloaded load_nhl_schedules(seasons) frame; None loads each season's schedule once.
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.

Returns

A polars (or pandas) DataFrame, one row per (season, as_of_date, team), ordered by season, then as_of_date, then team. No rated date returns a zero-row frame with this schema. |col_name |type | |:----------|:------| |season |Int64 | |as_of_date |Date | |team |String | |adj_xgf |Float64| |adj_xga |Float64| |adj_xg_net |Float64| |adj_gf |Float64| |adj_ga |Float64| |games |Int64 | |off_rank |Int64 | |def_rank |Int64 | |net_rank |Int64 | |net_z |Float64|

col_nametypedescription
seasonInt64Season's end year (2024 for 2023-24).
as_of_dateDateDate the snapshot is for; the ratings use only that season's regular-season games strictly before it.
teamStringTeam abbreviation (e.g. "TOR").
adj_xgfFloat64Opponent-adjusted even-strength xG for per game, shrunk toward the league mean by games played.
adj_xgaFloat64Opponent-adjusted even-strength xG against per game, shrunk toward the league mean by games played.
adj_xg_netFloat64adj_xgf minus adj_xga.
adj_gfFloat64Opponent-adjusted even-strength goals for per game, shrunk the same way (a sanity rating beside the xG one).
adj_gaFloat64Opponent-adjusted even-strength goals against per game, shrunk the same way.
gamesInt64Regular-season games the team played before as_of_date.
off_rankInt64Rank of adj_xgf among the teams rated on that date, 1 the highest.
def_rankInt64Rank of adj_xga among the teams rated on that date, 1 the lowest.
net_rankInt64Rank of adj_xg_net among the teams rated on that date, 1 the highest.
net_zFloat64adj_xg_net as a z-score across the teams rated on that date.

Raises

ExceptionDescription
ValueErrorleague has no constants.

Example

import polars as pl
from sportsdataverse.nhl.nhl_team_ratings import nhl_team_ratings_history

history = nhl_team_ratings_history(2024)
print(history.filter(pl.col("team") == "TOR").tail())

nhl_unit_ratings​

nhl_unit_ratings(pbp: 'pl.DataFrame', shifts: 'pl.DataFrame', *, model_dir: "'str | None'" = None, league: 'str' = 'nhl', unit_type: 'str' = 'forward_line', min_toi: 'float' = 20.0, return_as_pandas: 'bool' = False, xg_scored: 'bool' = False, _stints: 'pl.DataFrame | None' = None, _rapm: 'pl.DataFrame | None' = None) -> "'pl.DataFrame | pd.DataFrame'"

Per on-ice skater combination: observed xGF/xGA + shrinkage-blended summed RAPM.

Parameters

ParameterTypeDefaultDescription
pbpDataFramea load_nhl_pbp_full-shaped frame.
shiftsDataFramea load_nhl_shifts-shaped frame.
model_dirstr | NoneNonepassed through to nhl_xg/nhl_skater_rapm.
leaguestr'nhl'"nhl" or "pwhl".
unit_typestr'forward_line'"forward_line" (3-skater combinations) or "defense_pair" (2-skater combinations) -- see the module's data-availability caveat.
min_toifloat20.0minimum minutes-together for a unit to be reported.
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.
xg_scoredboolFalsepbp already carries the shot xg to use (another league's own xG model); skip re-scoring with the NHL boosters, which would replace it.
_stintsDataFrame | NoneNoneinternal test hook -- inject a pre-built stints frame.
_rapmDataFrame | NoneNoneinternal test hook -- inject a pre-built skater-RAPM frame (paired with _stints; both must be given together to bypass real computation).

Returns

team:Utf8, unit_ids:Utf8 (sorted "id-id-id"), unit_players:Utf8, toi_minutes:Float64, on_ice_xgf:Float64, on_ice_xga:Float64, on_ice_xgf_pct:Float64, summed_rapm:Float64, unit_value:Float64. Empty input returns a zero-row frame with this schema.

col_nametypedescription
teamStringTeam name.
unit_idsString
unit_playersString
toi_minutesFloat64Time on ice in minutes.
on_ice_xgfFloat64
on_ice_xgaFloat64
on_ice_xgf_pctFloat64
summed_rapmFloat64
unit_valueFloat64

Example

import polars as pl
from sportsdataverse.nhl.nhl_unit_ratings import nhl_unit_ratings
pbp = pl.read_parquet("tests/fixtures/nhl_player_impact/pbp_sample.parquet")
shifts = pl.read_parquet("tests/fixtures/nhl_player_impact/shifts_sample.parquet")
units = nhl_unit_ratings(pbp, shifts, model_dir="tests/fixtures/nhl_player_impact/xg_models")
print(units.sort("unit_value", descending=True).head(10))

nhl_xg​

nhl_xg(pbp: 'pl.DataFrame', *, model_dir: 'str | Path | None' = None, league: 'str' = 'nhl', return_as_pandas: 'bool' = False) -> "'pl.DataFrame | pd.DataFrame'"

Score every unblocked shot in pbp with the published nhl_xg_models boosters.

Ports fastRhockey's helper_nhl_calculate_xg -- routes 5v5 shots to the 5v5 booster and every other strength state to the special-teams booster, overrides penalty shots with the constant xg_model_ps, then left-joins xg back onto pbp by event_id. Attaches the danger/distance/angle expansion (add_shot_geometry) after scoring.

Known issue -- the published boosters over-predict for seasons through 2023-24. Measured 2026-09-02 against the 2026-04 boosters currently in the nhl_xg_models release: observed goals / sum(xg) is 0.771 at 5v5 (n=1,724,290 shots) and 0.768 on special teams (n=349,232), where a correctly-levelled model gives 1.0 -- i.e. xg is inflated by roughly 25-30% for every season from 2009-10 through 2023-24. At 5v5 the two most recent seasons are much closer (2024-25 0.949, 2025-26 0.913). The cause is not identified. It is not a defect in the feature frame this function builds, and it is not the missing-MISSED_SHOT training corpus recorded here previously: every season carries missed shots (27.7-34.8% of Fenwick events), the trainer's Fenwick selector takes MISSED_SHOT alongside SHOT and GOAL, and the published artifacts' base_score (0.07368 / 0.10533) matches the Fenwick goal rate (0.0695) rather than the shots-on-goal rate (0.0977). Leave-one-season-out refits land at goals / sum(xg) of 0.95-1.05 per season, so the miscalibration is a property of the published artifact rather than of the data it was trained on. Shot RANKING is far less affected (rank AUC 0.778 / 0.760), so xg is still usable for ordering chances -- but any SUM of xg (per game, per player, team totals, goals-above-expected, and nhl_gsax downstream) is inflated for pre-2024-25 seasons. Tracking: sportsdataverse-py#444; evidence: fastRhockey-nhl-data#11. To check whether this still applies to the boosters you have, restrict to the rows this function actually scored -- xg non-null, i.e. unblocked shots only -- and compare sum(xg) against the goals on those same rows, separately for strength_state == "5v5" and for the rest, since the two come from different boosters and are quoted separately above; a corrected booster gives a ratio near 1.0 for each. Comparing against a season's full goal total instead would fold in shootout and penalty-shot goals and every unscored row, and would not validate the numbers above. The same measurement is packaged as nhl_data_build.xg_parity.artifact_calibration(pbp, booster, variant=...) (variant is "5v5" or "st") in fastRhockey-nhl-data.

Parameters

ParameterTypeDefaultDescription
pbpDataFramea load_nhl_pbp_full-shaped frame.
model_dirstr | Path | NoneNonebooster directory; None downloads-and-caches on first use (see ensure_xg_models). Offline callers should pass the committed fixture dir.
leaguestr'nhl'"nhl" or "pwhl" -- selects the danger-zone geometry bands (the PWHL borrows the NHL boosters themselves; see xg_booster_league).
return_as_pandasboolFalsereturn a pandas DataFrame instead of polars.

Returns

pbp with xg:Float64, distance_to_net:Float64, shot_angle:Float64, shot_danger:Utf8 appended (null/absent for non-shot rows). Empty/malformed input returns the input frame with a null xg column -- never raises.

col_nametypedescription
event_typeStringStandardized event type code.
eventStringEvent description label.
secondary_typeStringSecondary event type (e.g. shot type).
event_team_abbrStringAbbreviation of the team credited with the event.
event_team_typeStringWhether the event team is home or away.
descriptionStringFull text description of the event.
periodInt64Period number.
period_typeStringPeriod type (REG/OT/SO).
period_timeStringElapsed time in the period (MM:SS).
period_secondsInt64Elapsed seconds in the period.
period_seconds_remainingInt64Seconds remaining in the period.
period_time_remainingStringTime remaining in the period (MM:SS).
game_secondsInt64Elapsed seconds in the game.
game_seconds_remainingInt64Seconds remaining in regulation.
home_scoreInt64Home team final score.
away_scoreInt64Away team final score.
event_player_1_nameStringName of the primary event player.
event_player_1_typeStringRole of the primary event player.
event_player_1_idInt64Player id of the primary event player.
event_player_2_nameStringName of the secondary event player.
event_player_2_typeStringRole of the secondary event player.
event_player_2_idInt64Player id of the secondary event player.
event_player_3_nameStringName of the tertiary event player.
event_player_3_typeStringRole of the tertiary event player.
event_player_3_idInt64Player ID of the tertiary event player.
event_goalie_nameStringName of the goalie on the event.
event_goalie_idInt64Player id of the goalie on the event.
penalty_severityStringSeverity of the penalty.
penalty_minutesInt64Penalty minutes.
strength_stateStringStrength state (e.g. 5v5, 5v4).
strength_codeStringStrength state code (e.g., all, even, pp, pk).
strengthStringStrength label (Even, Power Play, Shorthanded).
empty_netBooleanWhether the net was empty.
extra_attackerBooleanWhether an extra attacker was on the ice.
xInt64Raw x-coordinate of the event.
yInt64Raw y-coordinate of the event.
x_fixedInt64Normalized x coordinate (home shoots right).
y_fixedInt64Normalized y coordinate (home shoots right).
shot_distanceFloat64Distance of the shot from the net.
shot_angleFloat64Angle of the shot relative to the net.
home_skatersInt64Number of home skaters on the ice.
away_skatersInt64Number of away skaters on the ice.
home_on_1StringName of home skater 1 on the ice.
home_on_2StringName of home skater 2 on the ice.
home_on_3StringName of home skater 3 on the ice.
home_on_4StringName of home skater 4 on the ice.
home_on_5StringName of home skater 5 on the ice.
home_on_6StringName of home skater 6 on the ice.
home_on_7StringName of home skater 7 on the ice.
away_on_1StringName of away skater 1 on the ice.
away_on_2StringName of away skater 2 on the ice.
away_on_3StringName of away skater 3 on the ice.
away_on_4StringName of away skater 4 on the ice.
away_on_5StringName of away skater 5 on the ice.
away_on_6StringName of away skater 6 on the ice.
away_on_7StringName of away skater 7 on the ice.
home_goalieStringName of the home goalie on the ice.
away_goalieStringName of the away goalie on the ice.
num_onInt64Number of players coming on (line change).
players_onStringNames of players coming on.
num_offInt64Number of players going off (line change).
players_offStringNames of players going off.
game_idInt64Unique game identifier.
seasonStringSeason year (echoed from arg).
season_typeStringSeason type code (echoed from arg).
home_abbrStringHome team abbreviation.
away_abbrStringAway team abbreviation.
event_idxInt64Sequential event index within the game.
event_idInt64ESPN event id (echoed from arg).
pptReplayUrlStringURL to the play replay, if available.
away_goalie_inInt64Whether the away goalie is on the ice (1/0).
home_goalie_inInt64Whether the home goalie is on the ice (1/0).
reasonStringReason for the event (e.g. stoppage reason).
secondaryReasonStringSecondary reason for a stoppage.
ids_onStringPlayer ids coming on.
ids_offStringPlayer ids going off.
home_on_1_idInt64Player id of home skater 1 on the ice.
away_on_1_idInt64Player id of away skater 1 on the ice.
home_on_2_idInt64Player id of home skater 2 on the ice.
away_on_2_idInt64Player id of away skater 2 on the ice.
home_on_3_idInt64Player id of home skater 3 on the ice.
away_on_3_idInt64Player id of away skater 3 on the ice.
home_on_4_idInt64Player id of home skater 4 on the ice.
away_on_4_idInt64Player id of away skater 4 on the ice.
home_on_5_idInt64Player id of home skater 5 on the ice.
away_on_5_idInt64Player id of away skater 5 on the ice.
home_on_6_idInt64Player id of home skater 6 on the ice.
away_on_6_idInt64Player id of away skater 6 on the ice.
home_on_7_idInt64Player id of home skater 7 on the ice.
away_on_7_idInt64Player id of away skater 7 on the ice.
home_goalie_idInt64Player ID of the home goalie on the ice.
away_goalie_idInt64Player ID of the away goalie on the ice.
game_dateStringGame date.
xgFloat64Expected goals value for the shot event.
distance_to_netFloat64
shot_dangerString

Example

import polars as pl
from sportsdataverse.nhl.nhl_xg import nhl_xg
pbp = pl.read_parquet("tests/fixtures/nhl_player_impact/pbp_sample.parquet")
scored = nhl_xg(pbp, model_dir="tests/fixtures/nhl_player_impact/xg_models")
print(scored.filter(pl.col("xg").is_not_null()).height)

# Pandas round-trip

scored_pd = nhl_xg(pbp, return_as_pandas=True)