Skip to main content
Version: 0.2.0

Package — additional Python functions — ESPN

espn_depthcharts_snapshot​

espn_depthcharts_snapshot(league: 'str', *, team_ids: 'Optional[Sequence[Union[str, int]]]' = None, as_of_date: 'Optional[date]' = None, request_delay: 'float' = 1.5, return_as_pandas: 'bool' = False) -> 'Any'

Fetch today's depth chart for every team in a league, stamped with the date.

Unlike injuries, this endpoint is per team, so a league costs one request per team -- NFL 32, NBA 30, MLB 30. Requests are issued sequentially with request_delay seconds between them because ESPN answers aggressive polling with 403.

A team that publishes no depth chart contributes zero rows rather than a null-filled placeholder, so an empty observation can never be counted or persisted as data.

Parameters

ParameterTypeDefaultDescription
leaguestrLeague slug; see ESPN_DEPTHCHART_LEAGUES.
team_idsOptional[Sequence[Union[str, int]]]NoneTeams to fetch. Defaults to every team from espn_{league}_teams() (one extra request).
as_of_dateOptional[date]NoneObservation date stamped on every row; defaults to today (UTC).
request_delayfloat1.5Seconds to sleep between team requests.
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One frame with DEPTHCHART_SNAPSHOT_SCHEMA's columns, long over team.

Raises

ExceptionDescription
ValueErrorIf the league has no espn_{league}_team_depthcharts wrapper, or if team_ids was not supplied and espn_{league}_teams() returned no teams -- an empty directory is an upstream outage, and reporting it as zero rows would be indistinguishable from a league ESPN publishes no depth chart for. Passing team_ids=[] explicitly is a no-op, not an error.

Example

from sportsdataverse.espn_snapshots import espn_depthcharts_snapshot
df = espn_depthcharts_snapshot("nfl")

# Two teams only

df = espn_depthcharts_snapshot("nba", team_ids=[1, 2])

espn_injuries_snapshot​

espn_injuries_snapshot(leagues: 'Union[str, Sequence[str]]' = ('nfl', 'nba', 'wnba', 'nhl', 'mlb', 'cfb', 'mbb', 'wbb'), *, as_of_date: 'Optional[date]' = None, request_delay: 'float' = 1.5, return_as_pandas: 'bool' = False) -> 'Any'

Fetch today's injury report for one or more leagues, stamped with the date.

One request per league, issued sequentially with request_delay seconds between them: ESPN answers aggressive polling with 403, and this endpoint is cheap enough (one call per league per day) that there is nothing to gain from concurrency.

A league whose payload reports nothing contributes zero rows rather than a null-filled placeholder row, so an empty observation can never be counted or persisted as data.

Parameters

ParameterTypeDefaultDescription
leaguesUnion[str, Sequence[str]]('nfl', 'nba', 'wnba', 'nhl', 'mlb', 'cfb', 'mbb', 'wbb')One league slug or a sequence of them; defaults to ESPN_INJURY_LEAGUES.
as_of_dateOptional[date]NoneObservation date stamped on every row; defaults to today (UTC).
request_delayfloat1.5Seconds to sleep between league requests.
return_as_pandasboolFalseReturn a pandas DataFrame instead of polars.

Returns

One frame with INJURY_SNAPSHOT_SCHEMA's columns, long over league.

Raises

ExceptionDescription
ValueErrorIf a league slug has no espn_{league}_injuries wrapper.

Example

df = espn_injuries_snapshot("nfl")

# Every league that reports injuries

df = espn_injuries_snapshot(["nfl", "nba", "nhl", "mlb", "wnba", "cfb"])

pregame_state_as_of​

pregame_state_as_of(snapshots: 'Any', t: 'datetime | pl.Expr') -> 'Any'

Keep the snapshot rows that were known at decision time t.

A snapshot table stores each observed state as an interval: valid_from is the first capture that showed it, valid_to the first capture that no longer did (null while it is still current). A state is known at t iff valid_from <= t < valid_to, with a null valid_to read as open-ended. A row whose valid_from is after t is never returned, so a model that reads only this function's output cannot see a starter or an injury published after its decision time. The intervals already carry their end, so this is a filter, not an as-of join.

Parameters

ParameterTypeDefaultDescription
snapshotsAnyA polars DataFrame or LazyFrame with timezone-aware valid_from and valid_to datetime columns (any zone; compared as UTC instants), such as the nhl_pregame_snapshots release.
tdatetime | ExprThe decision time. A timezone-aware datetime applies one time to every row; a pl.Expr gives each row its own, for example puck drop minus three hours after a join to the schedule. The expression must also be timezone-aware.

Returns

The rows of snapshots valid at t, with the input's columns and frame type.

Raises

ExceptionDescription
ValueErrorIf valid_from or valid_to is missing, or t is a naive datetime (a naive time compared with a UTC column is a silent offset bug). A naive valid_from / valid_to column or pl.Expr raises polars' own error.

Example

from datetime import datetime, timedelta, timezone
import polars as pl
from sportsdataverse.espn_snapshots import pregame_state_as_of
from sportsdataverse.nhl import load_nhl_pregame_snapshots

snapshots = load_nhl_pregame_snapshots(seasons=2027)
t = datetime(2026, 11, 26, 0, 0, tzinfo=timezone.utc) - timedelta(hours=3)
known = pregame_state_as_of(snapshots, t)

# Each game's own lead, puck drop minus three hours

known = pregame_state_as_of(snapshots, pl.col("start_time_utc") - pl.duration(hours=3))

xfl_pbp​

xfl_pbp(game_id: 'Union[str, int]', *, return_as_pandas: 'bool' = False) -> "Union[pl.DataFrame, 'pd.DataFrame']"

Enriched XFL play-by-play (EP/EPA/WP/WPA/CP/CPOE).

Fetches the ESPN game summary, unrolls its drives into an nflverse-shape frame, and scores it with the same parity-validated NFL EP/WP pipeline used league-wide (see sportsdataverse.football.spring_football_ep_wp).

Parameters

ParameterTypeDefaultDescription
game_idUnion[str, int]ESPN XFL event id.
return_as_pandasboolFalseWhen True, return a pandas.DataFrame.

Returns

One row per play with ep/epa/wp/wpa/cp/cpoe and the other enrich_nfl_pbp output columns. Zero rows for a game ESPN has no play-by-play for.

col_nametypedescription
defteamString
defteam_scoreInt64
defteam_timeouts_remainingInt64
domeInt64
downInt64
game_halfString
game_idString
game_seconds_remainingFloat64
half_seconds_remainingFloat64
homeInt64
home_teamString
outdoorsInt64
play_idString
posteamString
posteam_scoreInt64
posteam_timeouts_remainingInt64
receive_2h_koInt64
retractableInt64
roofString
score_differentialInt64
seasonInt64
spread_lineFloat64
yardline_100Int64
ydstogoInt64
air_yardsFloat64
kickoff_attemptInt64
play_typeString
qtrInt64
epFloat64
td_probFloat64
opp_td_probFloat64
fg_probFloat64
opp_fg_probFloat64
safety_probFloat64
opp_safety_probFloat64
no_score_probFloat64
epaFloat64
qb_epaFloat64
air_epaFloat64
yac_epaFloat64
comp_air_epaFloat64
comp_yac_epaFloat64
total_home_comp_air_epaFloat64
total_away_comp_air_epaFloat64
total_home_comp_yac_epaFloat64
total_away_comp_yac_epaFloat64
total_home_raw_air_epaFloat64
total_away_raw_air_epaFloat64
total_home_raw_yac_epaFloat64
total_away_raw_yac_epaFloat64
posteam_spreadFloat64
elapsed_shareFloat64
spread_timeFloat64
Diff_Time_RatioFloat64
wpFloat64
vegas_wpFloat64
home_wpFloat64
away_wpFloat64
def_wpFloat64
vegas_home_wpaFloat64
vegas_wpaFloat64
wpaFloat64
air_wpaFloat64
yac_wpaFloat64
comp_air_wpaFloat64
comp_yac_wpaFloat64
total_home_comp_air_wpaFloat64
total_away_comp_air_wpaFloat64
total_home_comp_yac_wpaFloat64
total_away_comp_yac_wpaFloat64
total_home_raw_air_wpaFloat64
total_away_raw_air_wpaFloat64
total_home_raw_yac_wpaFloat64
total_away_raw_yac_wpaFloat64
cpFloat64
cpoeFloat64
xpassFloat64
pass_oeFloat64
xyac_epaFloat64
xyac_mean_yardageFloat64
xyac_median_yardageFloat64
xyac_successFloat64
xyac_fdFloat64
home_opening_kickoffFloat64
go_wpFloat64
first_down_probFloat64
wp_succeedFloat64
wp_failFloat64
fg_make_probFloat64
make_fg_wpFloat64
miss_fg_wpFloat64
fg_wpFloat64
punt_wpFloat64
go_boostFloat64
go_wp_diffFloat64
punt_wp_diffFloat64
fg_wp_diffFloat64
fourth_down_recommendationString

Example

from sportsdataverse.football.xfl import xfl_pbp

df = xfl_pbp("401517780")
print(df.select("play_id", "epa", "wp").head())