Skip to content

Data Types: Stats

MattsFace edited this page Jan 5, 2023 · 6 revisions

Usage that returns Stat objects

get_team_stats


Description: Returns stats for a team

Parameters:

Name Type Required Description Default
team_id string/int Yes description
stats list Yes Insert statType to return statistics for a players for a given sportId. Find available stat types at https://statsapi.mlb.com/api/v1/statTypes
groups list Yes Insert statGroup to return statistics for a given player based on group. Find available stat types at https://statsapi.mlb.com/api/v1/statGroup

Other Parameters:

Name Type Required Description Default
season string No Insert year to return team stats for a particular season.
sportIds string No Insert sportId to return team stats for a particular sportId.
gameType string No Insert gameType to return team stats for a particular gameType.
opposingTeamId string No Insert team id to return stats against a team. Used with vsTeam stat type.
opposingPlayerId string No Insert person id to return player stats for against a player. Used with vsPlayer stat type.

get_players_stats_for_game


Description: Return game stats for a player

Parameters:

Name Type Required Description Default
person_id string/int Yes person id for the stats
game_id string/int Yes game id for the stats

get_player_stats


Description: Return stats for a player

Parameters:

Name Type Required Description Default
person_id string Yes person id for the stats
stats list Yes Insert statType to return statistics for a players for a given sportId. Find available stat types at https://statsapi.mlb.com/api/v1/statTypes
groups list Yes Insert statGroup to return statistics for a given player based on group. Find available stat types at https://statsapi.mlb.com/api/v1/statGroup

Other Parameters:

Name Type Required Description Default
season string No Insert year to return player stats for a particular season.
sportIds string No Insert sportId to return player stats for a particular sportId.
gameType string No Insert gameType to return player stats for a particular gameType.
opposingTeamId string No Insert team id to return stats against a team. Used with vsTeam stat type.
opposingPlayerId string No Insert person id to return player stats for against a player. Used with vsPlayer stat type.

get_stats


Return standard statistics.

Parameters:

Name Type Required Description Default
stats list Yes Insert statType to return statistics for a players for a given sportId. Find available stat types at https://statsapi.mlb.com/api/v1/statTypes
groups list Yes Insert statGroup to return statistics for a given player based on group. Find available stat types at https://statsapi.mlb.com/api/v1/statGroup

Other Parameters:

Name Type Required Description Default
season string No Insert year to return player stats for a particular season.
sportIds string No Insert sportId to return player stats for a particular sportId.
gameType string No Insert gameType to return player stats for a particular gameType.
playerPool string No There are 4 different types of playerPools to return statistics for a particular playerPool across a sport. e.g All, Qualified, Rookies
position string No Insert position to return statistics for a given position. Default to "Qualified" playerPool. Find available positions at https://statsapi.mlb.com/api/v1/positions
teamId string No Insert teamId to return statistics for a given team. Default to "Qualified" playerPool.
leagueId string No Insert leagueId to return statistics for a given team. Default to "Qualified" playerPool.
opposingTeamId string No Insert team id to return stats against a team. Used with vsTeam stat type.
opposingPlayerId string No Insert person id to return player stats for against a player. Used with vsPlayer stat type.

Stats


Stat types.

Stats:

Name Type Required Description Default
season string No A season stat, supports stat groups hitting, pitching, fielding, and catching. Requires use of season param.
seasonAdvanced string No A seasonAdvanced stat, supports stat groups hitting, pitching, fielding, and catching.
career string No A career stat, supports stat groups hitting, pitching, fielding, and catching.
careerAdvanced string No A career advanced stat, supports stat groups hitting, pitching, fielding, and catching.
winLoss string No A winLoss stat, supports stat groups hitting, pitching.
winLossPlayoffs string No A winloss playoff stat, supports stat groups hitting, pitching.
homeAndAway string No A homeandaway stat, supports stat groups hitting, pitching.
homeAndAwayPlayoffs string No A winLoss stat, supports stat groups hitting, pitching.
careerRegularSeason string No A homeandaway playoffs stat, supports stat groups hitting, pitching.
careerPlayoffs string No A career playoff stat, supports stat groups hitting, pitching.
statsSingleSeason string No A season stat, supports stat groups hitting, pitching, fielding, and catching.
yearByYear string No A yearbyyear stat, supports stat groups hitting, pitching, fielding, and catching.
yearByYearPlayoffs string No A yearByYearPlayoffs stat, supports stat groups hitting, pitching, fielding, and catching.
opponentsFaced string No A opponentsFaced stat, supports stat groups hitting, pitching.
sabermetrics string No A sabermetrics stat, supports stat groups hitting, pitching.
gameLog string No A gameLog stat, supports stat groups hitting, pitching.
pitchLog string No A pitchLog stat, supports stat groups hitting, pitching.
playLog string No A playLog stat, supports stat groups hitting, pitching.
vsPlayer string No A vsPlayer stat, supports stat groups hitting, pitching. Requires use of opposingPlayerId param.
vsTeam string No A vsTeam stat, supports stat groups hitting, pitching. Requires use of opposingTeamId param.

Groups


Stat Groups.

Groups:

Name Type Required Description Default
hitting string No hitting stat group
pitching string No pitching stat group
fielding string No fielding stat group
catching string No catching stat group

Stat Structure

Attributes are expandable and collapsable - Link to Stat dataclass

group : str
  • type of the stat group
type : str
  • type of the stat
totalsplits : int
  • The number of split objects
exceptions : list
  • Sport code
splits : list
  • a list of split object
stat : Stats
  • stats of the split
season : int
  • Season of the split
numteams : str
  • number of teams for split
gametype : str
  • gametype of split
rank : int
  • rank of split
position : Position
  • position of the split
team : Team
  • team of the split
player : Person
  • player of the split
sport : Sport
  • sport of the split
league : League
  • league of the split

Example output from MLB stats api endpoint

Mlb stats api Query:

https://statsapi.mlb.com/api/v1/people/{id}/stats https://statsapi.mlb.com/api/v1/teams/{id}/stats

Equivelant with python-mlb-statsapi:

>>> mlb = mlbstatsapi.Mlb()
>>>
>>> player_id = mlb.get_people_id("Ty France")[0]
>>>
>>> stats = ['season', 'career']
>>> groups = ['hitting', 'pitching']
>>>
>>> stat_dict = mlb.get_player_stats(player_id, stats=stats, groups=groups )
>>> season_hitting_stat = stat_dict['hitting']['season']
>>> career_pitching_stat = stat_dict['pitching']['career']
>>>
>>> for split in season_hitting_stat.splits:
...     for k, v in split.stat.__dict__.items():
...             print(k, v)
gamesplayed 140
groundouts 163
airouts 148
runs 65
doubles 27
triples 1
homeruns 20
strikeouts 94
baseonballs 35
...
>>> for split in career_pitching_stat.splits:
...     for k, v in split.stat.__dict__.items():
...             print(k, v)
gamesplayed 2
gamesstarted 0
groundouts 2
airouts 4
runs 1
doubles 0
triples 0
homeruns 1
strikeouts 0
baseonballs 0
intentionalwalks 0
hits 2
hitbypitch 0
...
>>>
>>> team_id = mlb.get_team_id('Seattle Mariners')[0]
>>>
>>> stats = ['season', 'seasonAdvanced']
>>> groups = ['hitting']
>>>
>>> stats = mlb.get_team_stats(team_id, stats=stats, groups=groups)
>>> season_hitting = stats['hitting']['season']
>>> advanced_hitting = stats['hitting']['seasonadvanced']
>>>
>>>
>>> for split in season_hitting.splits:
...     for k, v in split.stat.__dict__.items():
...         print(k, v)
gamesplayed 162
groundouts 1273
airouts 1523
runs 690
doubles 229
triples 19
>>>
>>> for split in advanced_hitting.splits:
...     for k, v in split.stat.__dict__.items():
...         print(k, v)
...
plateappearances 6117
totalbases 2094
leftonbase 1129
sacbunts 9
sacflies 45