MLB Package
The MLB package offers multiple modules which can be used to retrieve information and statistics for Major League Baseball, such as team names, season stats, game schedules, and boxscore metrics.
Boxscore
The Boxscore module can be used to grab information from a specific game.
Metrics range from number of runs scored to the number of sacrifice flies, to
the slugging percentage and much more. The Boxscore can be easily queried by
passing a boxscore’s URI on sports-reference.com which can be retrieved from the
Schedule
class (see Schedule
module below for more information on
retrieving game-specific information).
from sportsipy.mlb.boxscore import Boxscore
game_data = Boxscore('BOS/BOS201808020')
print(game_data.home_runs) # Prints 15
print(game_data.away_runs) # Prints 7
df = game_data.dataframe # Returns a Pandas DataFrame of game metrics
The Boxscore module also contains a Boxscores
class which searches for all
games played on a particular day and returns a dictionary of matchups between
all teams on the requested day. The dictionary includes the names and
abbreviations for each matchup as well as the boxscore link if applicable.
from datetime import datetime
from sportsipy.mlb.boxscore import Boxscores
games_today = Boxscores(datetime.today())
print(games_today.games) # Prints a dictionary of all matchups for today
The Boxscores
class also allows the ability to query over a range of dates
using a second optional parameter during instantiation of the class. To query a
range of dates, enter the start date as the first parameter and the inclusive
end date as the second parameter.
from datetime import datetime
from sportsipy.mlb.boxscore import Boxscores
# Pulls all games between and including July 17, 2017 and July 20, 2017
games = Boxscores(datetime(2017, 7, 17), datetime(2017, 7, 20))
# Prints a dictionary of all results from July 17, 2017 and July 20, 2017
print(games.games)
- class sportsipy.mlb.boxscore.Boxscore(uri)[source]
Bases:
object
Detailed information about the final statistics for a game.
Stores all relevant information for a game such as the date, time, location, result, and more advanced metrics such as the number of strikes, a pitcher’s influence on the game, the number of putouts and much more.
- Parameters
uri (string) – The relative link to the boxscore HTML page, such as ‘BOS/BOS201806070’.
- property attendance
Returns an
int
of the game’s listed attendance.
- property away_assists
Returns an
int
of the number of assists the away team registered.
- property away_at_bats
Returns an
int
of the number of at bats the away team had.
- property away_average_leverage_index
Returns a
float
of the amount of pressure the away team’s pitcher faced during the game. 1.0 denotes average pressure while numbers less than 0 denote lighter pressure.
- property away_base_out_runs_added
Returns a
float
of the number of base out runs added by the away team.
- property away_base_out_runs_saved
Returns a
float
of the number of runs saved by the away pitcher based on the number of players on bases. 0.0 denotes an average value.
- property away_bases_on_balls
Returns an
int
of the number of bases the away team registerd as a result of balls.
- property away_batting_average
Returns a
float
of the batting average for the away team.
- property away_earned_runs
Returns a
float
of the number of runs the away team earned.
- property away_fly_balls
Returns an
int
of the number of fly balls the away team allowed.
- property away_game_score
Returns an
int
of the starting away pitcher’s score determine by many factors, such as number of runs scored against, number of strikes, etc.
- property away_grounded_balls
Returns an
int
of the number of grounded balls the away team allowed.
- property away_hits
Returns an
int
of the number of hits the away team had.
- property away_home_runs
Returns an
int
of the number of times the away team gave up a home run.
- property away_inherited_runners
Returns an
int
of the number of runners a pitcher inherited when he entered the game.
- property away_inherited_score
Returns an
int
of the number of scorers a pitcher inherited when he entered the game.
- property away_innings_pitched
Returns a
float
of the number of innings the away team pitched.
- property away_line_drives
Returns an
int
of the number of line drives the away team allowed.
- property away_on_base_percentage
Returns a
float
of the percentage of at bats that result in the batter getting on base.
- property away_on_base_plus
Returns a
float
of the on base percentage plus the slugging percentage. Percentage ranges from 0-1.
- property away_pitches
Returns an
int
of the number of pitches the away team faced.
- property away_plate_appearances
Returns an
int
of the number of plate appearances the away team made.
- property away_players
Returns a
list
ofBoxscorePlayer
class instances for each player on the away team.
- property away_putouts
Returns an
int
of the number of putouts the away team registered.
- property away_rbi
Returns an
int
of the number of runs batted in the away team registered.
- property away_runs
Returns an
int
of the number of runs the away team scored.
- property away_slugging_percentage
Returns a
float
of the slugging percentage for the away team based on the number of bases gained per at-bat with bigger plays getting more weight.
- property away_strikeouts
Returns an
int
of the number of times the away team was struck out.
- property away_strikes
Returns an
int
of the number of times a strike was called against the away team.
- property away_strikes_by_contact
Returns an
int
of the number of times the away team struck out a batter who made contact with the pitch.
- property away_strikes_looking
Returns an
int
of the number of times the away team struck out a batter who was looking.
- property away_strikes_swinging
Returns an
int
of the number of times the away team struck out a batter who was swinging.
- property away_unknown_bat_type
Returns an
int
of the number of away at bats that were not properly tracked and therefore cannot be safely placed in another statistical category.
- property away_win_probability_added
Returns a
float
of the total positive influence the away team’s offense had on the outcome of the game.
- property away_win_probability_by_pitcher
Returns a
float
of the amount of influence the away pitcher had on the game’s result with 0.0 denoting zero influence and 1.0 denoting he was solely responsible for the team’s win.
- property away_win_probability_for_offensive_player
Returns a
float
of the overall influence the away team’s offense had on the outcome of the game where 0.0 denotes no influence and 1.0 denotes the offense was solely responsible for the outcome.
- property away_win_probability_subtracted
Returns a
float
of the total negative influence the away team’s offense had on the outcome of the game.
- property dataframe
Returns a pandas DataFrame containing all other class properties and values. The index for the DataFrame is the string URI that is used to instantiate the class, such as ‘BOS201806070’.
- property date
Returns a
string
of the date the game took place.
- property duration
MM’.
- Type
Returns a
string
of the game’s duration in the format ‘H
- property home_assists
Returns an
int
of the number of assists the home team registered.
- property home_at_bats
Returns an
int
of the number of at bats the home team had.
- property home_average_leverage_index
Returns a
float
of the amount of pressure the home team’s pitcher faced during the game. 1.0 denotes average pressure while numbers less than 0 denote lighter pressure.
- property home_base_out_runs_added
Returns a
float
of the number of base out runs added by the home team.
- property home_base_out_runs_saved
Returns a
float
of the number of runs saved by the home pitcher based on the number of players on bases. 0.0 denotes an average value.
- property home_bases_on_balls
Returns an
int
of the number of bases the home team registerd as a result of balls.
- property home_batting_average
Returns a
float
of the batting average for the home team.
- property home_earned_runs
Returns a
float
of the number of runs the home team earned.
- property home_fly_balls
Returns an
int
of the number of fly balls the home team allowed.
- property home_game_score
Returns an
int
of the starting home pitcher’s score determine by many factors, such as number of runs scored against, number of strikes, etc.
- property home_grounded_balls
Returns an
int
of the number of grounded balls the home team allowed.
- property home_hits
Returns an
int
of the number of hits the home team had.
- property home_home_runs
Returns an
int
of the number of times the home team gave up a home run.
- property home_inherited_runners
Returns an
int
of the number of runners a pitcher inherited when he entered the game.
- property home_inherited_score
Returns an
int
of the number of scorers a pitcher inherited when he entered the game.
- property home_innings_pitched
Returns a
float
of the number of innings the home team pitched.
- property home_line_drives
Returns an
int
of the number of line drives the home team allowed.
- property home_on_base_percentage
Returns a
float
of the percentage of at bats that result in the batter getting on base.
- property home_on_base_plus
Returns a
float
of the on base percentage plus the slugging percentage. Percentage ranges from 0-1.
- property home_pitches
Returns an
int
of the number of pitches the home team faced.
- property home_plate_appearances
Returns an
int
of the number of plate appearances the home team made.
- property home_players
Returns a
list
ofBoxscorePlayer
class instances for each player on the home team.
- property home_putouts
Returns an
int
of the number of putouts the home team registered.
- property home_rbi
Returns an
int
of the number of runs batted in the home team registered.
- property home_runs
Returns an
int
of the number of runs the home team scored.
- property home_slugging_percentage
Returns a
float
of the slugging percentage for the home team based on the number of bases gained per at-bat with bigger plays getting more weight.
- property home_strikeouts
Returns an
int
of the number of times the home team was struck out.
- property home_strikes
Returns an
int
of the number of times a strike was called against the home team.
- property home_strikes_by_contact
Returns an
int
of the number of times the home team struck out a batter who made contact with the pitch.
- property home_strikes_looking
Returns an
int
of the number of times the home team struck out a batter who was looking.
- property home_strikes_swinging
Returns an
int
of the number of times the home team struck out a batter who was swinging.
- property home_unknown_bat_type
Returns an
int
of the number of home at bats that were not properly tracked and therefore cannot be safely placed in another statistical category.
- property home_win_probability_added
Returns a
float
of the total positive influence the home team’s offense had on the outcome of the game.
- property home_win_probability_by_pitcher
Returns a
float
of the amount of influence the home pitcher had on the game’s result with 0.0 denoting zero influence and 1.0 denoting he was solely responsible for the team’s win.
- property home_win_probability_for_offensive_player
Returns a
float
of the overall influence the home team’s offense had on the outcome of the game where 0.0 denotes no influence and 1.0 denotes the offense was solely responsible for the outcome.
- property home_win_probability_subtracted
Returns a
float
of the total negative influence the home team’s offense had on the outcome of the game.
- property losing_abbr
Returns a
string
of the losing team’s abbreviation, such as ‘LAD’ for the Los Angeles Dodgers.
- property losing_name
Returns a
string
of the losing team’s name, such as ‘Los Angeles Dodgers’.
- property summary
Returns a
dictionary
with two keys, ‘away’ and ‘home’. The value of each key will be a list for each respective team’s score by order of the inning, with the first element belonging to the first inning, similar to the following:- {
‘away’: [5, 0, 1, 0, 0, 0, 0, 1, 0], ‘home’: [1, 0, 0, 0, 1, 0, 0, 0, 0]
}
- property time
Returns a
string
of the time the game started.
- property time_of_day
Returns a
string
constant indicated whether the game was played during the day or at night.
- property venue
Returns a
string
of the name of the ballpark where the game was played.
- property winner
Returns a
string
constant indicating whether the home or away team won.
- property winning_abbr
Returns a
string
of the winning team’s abbreviation, such as ‘HOU’ for the Houston Astros.
- property winning_name
Returns a
string
of the winning team’s name, such as ‘Houston Astros’.
- class sportsipy.mlb.boxscore.BoxscorePlayer(player_id, player_name, player_data)[source]
Bases:
sportsipy.mlb.player.AbstractPlayer
Get player stats for an individual game.
Given a player ID, such as ‘altuvjo01’ for Jose Altuve, their full name, and all associated stats from the Boxscore page in HTML format, parse the HTML and extract only the relevant stats for the specified player and assign them to readable properties.
This class inherits the
AbstractPlayer
class. As a result, all properties associated withAbstractPlayer
can also be read directly from this class.As this class is instantiated from within the Boxscore class, it should not be called directly and should instead be queried using the appropriate players properties from the Boxscore class.
- Parameters
player_id (string) – A player’s ID according to baseball-reference.com, such as ‘altuvjo01’ for Jose Altuve. The player ID can be found by navigating to the player’s stats page and getting the string between the final slash and the ‘.html’ in the URL. In general, the ID is in the format ‘LLLLLFFNN’ where ‘LLLLL’ are the first 5 letters in the player’s last name, ‘FF’, are the first 2 letters in the player’s first name, and ‘NN’ is a number starting at ‘01’ for the first time that player ID has been used and increments by 1 for every successive player.
player_name (string) – A string representing the player’s first and last name, such as ‘Jose Altuve’.
player_data (string) – A string representation of the player’s HTML data from the Boxscore page. If the player appears in multiple tables, all of their information will appear in one single string concatenated together.
- property average_leverage_index
Returns a
float
of the amount of pressure the player faced during the game. 1.0 denotes average pressure while numbers less than 0 denote lighter pressure.
- property average_leverage_index_pitcher
Returns a
float
of the amount of pressure the pitcher faced during the game. 1.0 denotes average pressure while numbers less than 0 denote lighter pressure.
- property base_out_runs_added
Returns a
float
of the number of base out runs added by the player.
- property base_out_runs_saved
Returns a
float
of the number of runs saved by the pitcher based on the number of players on bases. 0.0 denotes an average value.
- property dataframe
Returns a
pandas DataFrame
containing all other relevant class properties and values for the specified game.
- property earned_runs_against
Returns a
float
of the player’s overall Earned Runs Against average as calculated by 9 * earned_runs / innings_pitched.
- property fly_balls
Returns an
int
of the number of fly balls the player allowed.
- property game_score
Returns an
int
of the pitcher’s score determine by many factors, such as number of runs scored against, number of strikes, etc.
- property grounded_balls
Returns an
int
of the number of grounded balls the player allowed.
- property home_runs_thrown
Returns an
int
of the number of home runs the player threw.
- property inherited_runners
Returns an
int
of the number of runners a relief pitcher inherited.
- property inherited_score
Returns an
int
of the number of runners on base when a relief pitcher entered the game that ended up scoring.
- property innings_pitched
Returns a
float
of the number of innings the player pitched in. Numbers ending in ‘.0’ indicate complete innings, while numbers ending in ‘.1’ are for 1/3 of an inning, and ‘.2’ is for 2/3 of an inning.
- property line_drives
Returns an
int
of the number of line drives the player allowed.
- property pitches_thrown
Returns an
int
of the number of pitches the player threw.
- property strikes
Returns an
int
of the number of times a strike was called against the player.
- property strikes_contact
Returns an
int
of the number of times the player threw a strike when the player made contact with the ball.
- property strikes_looking
Returns an
int
of the number of times the player threw a strike with the player looking.
- property strikes_swinging
Returns an
int
of the number of times the player threw a strike with the batter swinging.
- property strikes_thrown
Returns an
int
of the number of times a strikes the player threw.
- property unknown_bat_types
Returns an
int
of the number of line drives the player allowed.
- property win_probability_added
Returns a
float
of the total positive influence the player’s offense had on the outcome of the game.
- property win_probability_added_pitcher
Returns a
float
of the total positive influence the pitcher’s offense had on the outcome of the game.
- property win_probability_for_offensive_player
Returns a
float
of the overall influence the player’s offense had on the outcome of the game where 0.0 denotes no influence and 1.0 denotes the offense was solely responsible for the outcome.
- property win_probability_subtracted
Returns a
float
of the total negative influence the player’s offense had on the outcome of the game.
- class sportsipy.mlb.boxscore.Boxscores(date, end_date=None)[source]
Bases:
object
Search for MLB games taking place on a particular day.
Retrieve a dictionary which contains a list of all games being played on a particular day. Output includes a link to the boxscore, and the names and abbreviations for both the home teams. If no games are played on a particular day, the list will be empty.
- Parameters
date (datetime object) – The date to search for any matches. The month, day, and year are required for the search, but time is not factored into the search.
end_date (datetime object (optional)) – Optionally specify an end date to iterate until. All boxscores starting from the date specified in the ‘date’ parameter up to and including the boxscores specified in the ‘end_date’ parameter will be pulled. If left empty, or if ‘end_date’ is prior to ‘date’, only the games from the day specified in the ‘date’ parameter will be saved.
- property games
Returns a
dictionary
object representing all of the games played on the requested day. Dictionary is in the following format:{ 'date': [ # 'date' is the string date in format 'MM-DD-YYYY' { 'home_name': Name of the home team, such as 'New York Yankees' (`str`), 'home_abbr': Abbreviation for the home team, such as 'NYY' (`str`), 'away_name': Name of the away team, such as 'Houston Astros' (`str`), 'away_abbr': Abbreviation for the away team, such as 'HOU' (`str`), 'boxscore': String representing the boxscore URI, such as 'SLN/SLN201807280' (`str`), 'winning_name': Full name of the winning team, such as 'New York Yankees' (`str`), 'winning_abbr': Abbreviation for the winning team, such as 'NYY' (`str`), 'losing_name': Full name of the losing team, such as 'Houston Astros' (`str`), 'losing_abbr': Abbreviation for the losing team, such as 'HOU' (`str`), 'home_score': Integer score for the home team (`int`), 'away_score': Integer score for the away team (`int`) }, { ... }, ... ] }
If no games were played on ‘date’, the list for [‘date’] will be empty.
Player
The Player module contains an abstract base class that can be inherited by both
the BoxscorePlayer
and Player
classes in the Boxscore
and Roster
modules, respectively. All of the properties that appear in the
AbstractPlayer
class can be read from either of the two child classes
mentioned above.
- class sportsipy.mlb.player.AbstractPlayer(player_id, player_name, player_data)[source]
Bases:
object
Get player information and stats for all seasons.
Given a player ID, such as ‘altuvjo01’ for Jose Altuve, capture all relevant stats and information like name, nationality, height/weight, career home runs, last season’s batting average, salary, contract amount, and much more.
By default, the class instance will return the player’s career stats, but single-season stats can be found by calling the instance with the requested season as denoted on baseball-reference.com.
- Parameters
player_id (string) – A player’s ID according to basketball-reference.com, such as ‘altuvjo01’ for Jose Altuve. The player ID can be found by navigating to the player’s stats page and getting the string between the final slash and the ‘.html’ in the URL. In general, the ID is in the format ‘LLLLLFFNN’ where ‘LLLLL’ are the first 5 letters in the player’s last name, ‘FF’, are the first 2 letters in the player’s first name, and ‘NN’ is a number starting at ‘01’ for the first time that player ID has been used and increments by 1 for every successive player.
- property assists
Returns an
int
of the number of assists the player had.
- property at_bats
Returns an
int
of the number of at bats the player had.
- property bases_on_balls
Returns an
int
of the number of bases the player registered as a result of balls.
- property bases_on_balls_given
Returns an
int
of the number of bases on balls the player has given as a pitcher.
- property batters_faced
Returns an
int
of the number of batters the pitcher has faced.
- property batting_average
Returns a
float
of the batting average for the player.
- property earned_runs_allowed
Returns an
int
of the number of earned runs the player allowed as a pitcher.
- property hits
Returns an
int
of the number of hits the player had.
- property hits_allowed
Returns an
int
of the number of hits the player allowed as a pitcher.
- property name
Returns a
string
of the player’s name, such as ‘Jose Altuve’.
- property on_base_percentage
Returns a
float
of the percentage of at bats that result in the batter getting on base.
- property on_base_plus_slugging_percentage
Returns a
float
of the on base percentage plus the slugging percentage. Percentage ranges from 0-1.
- property plate_appearances
Returns an
int
of the number of plate appearances the player had.
- property player_id
Returns a
string
of the player’s ID on sports-reference, such as ‘altuvjo01’ for Jose Altuve.
- property putouts
Returns an
int
of the number of putouts the player had.
- property runs
Returns an
int
of the number of runs the player scored.
- property runs_allowed
Returns an
int
of the number of runs the player allowed as a pitcher.
- property runs_batted_in
Returns an
int
of the number of runs batted in the player registered.
- property slugging_percentage
Returns a
float
of the slugging percentage for the player based on the number of bases gained per at-bat with bigger plays getting more weight.
- property strikeouts
Returns an
int
of the number of strikeouts the player threw as a pitcher.
- property times_struck_out
Returns an
int
of the number of times the player was struck out.
Roster
The Roster module contains detailed player information, allowing each player to
be queried by their player ID using the Player
class which has detailed
information ranging from career home runs to single-season stats and player
height, weight, and nationality. The following is an example on collecting
career information for José Altuve.
from sportsipy.mlb.roster import Player
altuve = Player('altuvjo01')
print(altuve.name) # Prints 'José Altuve'
print(altuve.hits) # Prints Altuve's career hits total
# Prints a Pandas DataFrame of all relevant stats per season for Altuve
print(altuve.dataframe)
By default, the player’s career stats are returned whenever a property is called. To get stats for a specific season, call the class instance with the season string. All future property requests will return the season-specific stats.
from sportsipy.mlb.roster import Player
altuve = Player('altuvjo01') # Currently pulling career stats
print(altuve.hits) # Prints Altuve's CAREER hits total
# Prints Altuve's hits total only for the 2017 season
print(altuve('2017').hits)
# Prints Altuve's home runs total for the 2017 season only
print(altuve.home_runs)
After requesting single-season stats, the career stats can be requested again by calling the class without arguments or with the ‘Career’ string passed.
from sportsipy.mlb.roster import Player
altuve = Player('altuvjo01') # Currently pulling career stats
# Prints Altuve's hits total only for the 2017 season
print(altuve('2017').hits)
print(altuve('Career').hits) # Prints Altuve's career hits total
In addition, the Roster module also contains the Roster
class which can be
used to pull all players on a team’s roster during a given season and creates
instances of the Player class for each team member and adds them to a list to be
easily queried.
from sportsipy.mlb.roster import Roster
astros = Roster('HOU')
for player in astros.players:
# Prints the name of all players who played for the Astros in the most
# recent season.
print(player.name)
- class sportsipy.mlb.roster.Player(player_id)[source]
Bases:
sportsipy.mlb.player.AbstractPlayer
Get player information and stats for all seasons.
Given a player ID, such as ‘altuvjo01’ for Jose Altuve, capture all relevant stats and information like name, nationality, height/weight, career home runs, last season’s batting average, salary, contract amount, and much more.
By default, the class instance will return the player’s career stats, but single-season stats can be found by calling the instance with the requested season as denoted on baseball-reference.com.
- Parameters
player_id (string) – A player’s ID according to basketball-reference.com, such as ‘altuvjo01’ for Jose Altuve. The player ID can be found by navigating to the player’s stats page and getting the string between the final slash and the ‘.html’ in the URL. In general, the ID is in the format ‘LLLLLFFNN’ where ‘LLLLL’ are the first 5 letters in the player’s last name, ‘FF’, are the first 2 letters in the player’s first name, and ‘NN’ is a number starting at ‘01’ for the first time that player ID has been used and increments by 1 for every successive player.
- property assists
Returns an
int
of the number of assists the player had.
- property at_bats
Returns an
int
of the number of at bats the player had.
- property balks
Returns an
int
of the number of times the pitcher balked.
- property bases_on_balls
Returns an
int
of the number of bases the player registered as a result of balls.
- property bases_on_balls_given
Returns an
int
of the number of bases on balls the player has given as a pitcher.
- property bases_on_balls_given_per_nine_innings
Returns a
float
of the number of bases on balls the pitcher has given per nine innings played.
- property batters_struckout_per_nine_innings
Returns a
float
of the number of batters the pitcher has struck out per nine innings played.
- property batting_average
Returns a
float
of the batting average for the player.
- property birth_date
Returns a
datetime
object of the day and year the player was born.
- property complete_games
Returns an
int
of the number of complete games the player has participated in.
- property contract
Returns a
dictionary
of the player’s contract where each key is astring
of the year, such as ‘2017’ and each value is adictionary
with thestring
key-value pairs of the player’s age, team name, and salary.
- property dataframe
Returns a
pandas DataFrame
containing all other relevant class properties and values where each index is a different season plus the career stats.
- property defensive_chances
Returns an
int
of the number of defensive chances (equal to the number of putouts + assists + errors) the player had.
- property defensive_runs_saved_above_average
Returns an
int
of the number of defensive runs the player saved compared to an average player.
- property defensive_runs_saved_above_average_per_innings
Returns an
int
of the number of defensive runs the player was worth per 1,200 innings compared to an average player.
- property double_plays_turned
Returns an
int
of the number of double plays the player was involved in.
- property doubles
Returns an
int
of the number of doubles the player hit.
- property earned_runs_allowed
Returns an
int
of the number of earned runs the player allowed as a pitcher.
- property era
Returns a
float
of the pitcher’s Earned Runs Average.
- property era_plus
Returns a
float
of the pitcher’s ERA while adjusted for the ballpark.
- property errors
Returns an
int
of the number of errors the player made.
- property fielding_independent_pitching
Returns a
float
of the pitcher’s effectiveness at preventing home runs, bases on balls, and hitting players with pitches, while causing strikeouts.
- property fielding_percentage
Returns a
float
of the players fielding percentage, equivalent to (putouts + assists) / (putouts + assists + errors). Percentage ranges from 0-1.
- property games
Returns an
int
of the number of games the player participated in.
- property games_catcher
Returns an
int
of the number of games the player was in the lineup as a catcher.
- property games_center_fielder
Returns an
int
of the number of games the player was in the lineup as a center fielder.
- property games_designated_hitter
Returns an
int
of the number of games the player was in the lineup as a designated hitter.
- property games_finished
Returns an
int
of the number of games the player finished as a pitcher.
- property games_first_baseman
Returns an
int
of the number of games the player was in the lineup as a first baseman.
- property games_in_batting_order
Returns an
int
of the number of games the player was in the batting lineup.
- property games_in_defensive_lineup
Returns an
int
of the number of games the player was in the defensive lineup.
- property games_left_fielder
Returns an
int
of the number of games the player was in the lineup as a left fielder.
- property games_outfielder
Returns an
int
of the number of games the player was in the lineup as an outfielder.
- property games_pinch_hitter
Returns an
int
of the number of games the player was in the lineup as a pinch hitter.
- property games_pinch_runner
Returns an
int
of the number of games the player was in the lineup as a pinch runner.
- property games_pitcher
Returns an
int
of the number of games the player was in the lineup as a pitcher.
- property games_right_fielder
Returns an
int
of the number of games the player was in the lineup as a right fielder.
- property games_second_baseman
Returns an
int
of the number of games the player was in the lineup as a second baseman.
- property games_shortstop
Returns an
int
of the number of games the player was in the lineup as a shortstop.
- property games_started
Returns an
int
of the number of games the player started.
- property games_third_baseman
Returns an
int
of the number of games the player was in the lineup as a third baseman.
- property grounded_into_double_plays
Returns an
int
of the number of double plays the player grounded into.
- property height
Returns a
string
of the players height in the format “feet-inches”.
- property hits
Returns an
int
of the number of hits the player had.
- property hits_against_per_nine_innings
Returns a
float
of the number of hits the player has given per nine innings played.
- property hits_allowed
Returns an
int
of the number of hits the player allowed as a pitcher.
- property home_runs
Returns an
int
of the number of home runs the player hit.
- property home_runs_against_per_nine_innings
Returns a
float
of the number of home runs the pitcher has given per nine innings played.
- property home_runs_allowed
Returns an
int
of the number of home runs a player has allowed as a pitcher.
- property innings_played
Returns a
float
of the total number of innings the player has played in.
- property intentional_bases_on_balls
Returns an
int
of the number of times the player has been intentionally walked by the opposition.
- property intentional_bases_on_balls_given
Returns an
int
of the number of bases the player has intentionally given as a pitcher.
- property league_fielding_percentage
Returns a
float
of the average fielding percentage for the league at the player’s position. Percentage ranges from 0-1.
- property league_range_factor_per_game
Returns a
float
of the average range factor for the league per game, equal to (putouts + assists) / games_played.
- property league_range_factor_per_nine_innings
Returns a
float
of the average range factor for the league per nine innings, equal to 9 * (putouts + assists) / innings_played.
- property losses
Returns an
int
of the number of games the player has lost as a pitcher.
- property name
Returns a
string
of the player’s name, such as ‘Jose Altuve’.
- property nationality
Returns a
string
constant denoting which country the player originiates from.
- property on_base_percentage
Returns a
float
of the percentage of at bats that result in the batter getting on base.
- property on_base_plus_slugging_percentage
Returns a
float
of the on base percentage plus the slugging percentage. Percentage ranges from 0-1.
- property on_base_plus_slugging_percentage_plus
Returns an
int
of the on base percentage plus the slugging percentage, adjusted to the player’s ballpark.
- property plate_appearances
Returns an
int
of the number of plate appearances the player had.
- property position
Returns a
string
constant of the player’s primary position.
- property putouts
Returns an
int
of the number of putouts the player had.
- property range_factor_per_game
Returns a
float
of the players range factor per game, equal to 9 * (putouts + assists) / games_played.
- property range_factor_per_nine_innings
Returns a
float
of the players range factor per nine innings, equal to 9 * (putouts + assists) / innings_played.
- property runs
Returns an
int
of the number of runs the player scored.
- property runs_allowed
Returns an
int
of the number of runs the player allowed as a pitcher.
- property runs_batted_in
Returns an
int
of the number of runs batted in the player registered.
- property sacrifice_flies
Returns an
int
of the number of sacrifice flies the player hit.
- property sacrifice_hits
Returns an
int
of the number of sacrifice hits or sacrafice bunts the player made.
- property saves
Returns an
int
of the number of saves the player made as a pitcher.
- property season
Returns a
string
of the season in the format ‘YYYY’, such as ‘2017’. If no season was requsted, the career stats will be returned for the player and the season will default to ‘Career’.
- property shutouts
Returns an
int
of the number of times the player did not allow any runs and threw a complete game as a pitcher.
- property slugging_percentage
Returns a
float
of the slugging percentage for the player based on the number of bases gained per at-bat with bigger plays getting more weight.
- property stolen_bases
Returns an
int
of the number of bases the player has stolen.
- property strikeouts
Returns an
int
of the number of strikeouts the player threw as a pitcher.
- property strikeouts_thrown_per_walk
Returns a
float
of the number of batters the pitcher has struck out per the number of walks given.
- property team_abbreviation
Returns a
string
of the team’s abbreviation, such as ‘HOU’ for the Houston Astros.
- property times_caught_stealing
Returns an
int
of the number of times the player was caught stealing.
- property times_hit_by_pitch
Returns an
int
of the number of times the player has been hit by a pitch.
- property times_hit_player
Returns an
int
of the number of times the pitcher hit a player with a pitch.
- property times_struck_out
Returns an
int
of the number of times the player was struck out.
- property total_bases
Returns an
int
of the number of bases the player has gained.
- property total_fielding_runs_above_average
Returns an
int
of the number of runs the player was worth compared to an average player.
- property total_fielding_runs_above_average_per_innings
Returns an
int
of the number of runs the player was worth per 1,200 innings compared to an average player.
- property triples
Returns an
int
of the number of triples the player hit.
- property weight
Returns an
int
of the player’s weight in pounds.
- property whip
Returns a
float
of the pitcher’s WHIP score, equivalent to (bases on balls + hits) / innings played.
- property wild_pitches
Returns an
int
of the number of wild pitches the player has thrown.
- property win_percentage
Returns a
float
of the players winning percentage as a pitcher. Percentage ranges from 0-1.
- property wins
Returns an
int
of the number of games the player has won as a pitcher.
- class sportsipy.mlb.roster.Roster(team, year=None, slim=False)[source]
Bases:
object
Get stats for all players on a roster.
Request a team’s roster for a given season and create instances of the Player class for each player, containing a detailed list of the players statistics and information.
- Parameters
team (string) – The team’s abbreviation, such as ‘HOU’ for the Houston Astros.
year (string (optional)) – The 4-digit year to pull the roster from, such as ‘2018’. If left blank, defaults to the most recent season.
slim (boolean (optional)) – Set to True to return a limited subset of player information including the name and player ID for each player as opposed to all of their respective stats which greatly reduces the time to return a response if just the names and IDs are desired. Defaults to False.
- property coach
Returns a
string
of the coach’s name, such as ‘AJ Hinch’.
- property players
Returns a
list
of player instances for each player on the requested team’s roster if theslim
property is False when calling the Roster class. If theslim
property is True, returns adictionary
where each key is a string of the player’s ID and each value is the player’s first and last name as listed on the roster page.
Schedule
The Schedule module can be used to iterate over all games in a team’s schedule
to get game information such as the date, score, result, and more. Each game
also has a link to the Boxscore
class which has much more detailed
information on the game metrics.
from sportsipy.mlb.schedule import Schedule
houston_schedule = Schedule('HOU')
for game in houston_schedule:
print(game.date) # Prints the date the game was played
print(game.result) # Prints whether the team won or lost
# Creates an instance of the Boxscore class for the game.
boxscore = game.boxscore
- class sportsipy.mlb.schedule.Game(game_data, year)[source]
Bases:
object
A representation of a matchup between two teams.
Stores all relevant high-level match information for a game in a team’s schedule including date, time, opponent, and result.
- Parameters
game_data (string) – The row containing the specified game information.
year (string) – The year of the current season.
- property attendance
Returns an
int
of the total listed attendance for the game.
- property boxscore
Returns an instance of the Boxscore class containing more detailed stats on the game.
- property boxscore_index
Returns a
string
of the URI for a boxscore which can be used to access or index a game.
- property dataframe
Returns a pandas DataFrame containing all other class properties and values. The index for the DataFrame is the boxscore string.
- property dataframe_extended
Returns a pandas DataFrame representing the Boxscore class for the game. This property provides much richer context for the selected game, but takes longer to process compared to the lighter ‘dataframe’ property. The index for the DataFrame is the boxscore string.
- property date
Returns a
string
of the date the game was played on.
- property datetime
Returns a datetime object of the month, day, year, and time the game was played.
- property day_or_night
Returns a
string
constant to indicate whether the game was played during the day or night.
- property game
Returns an
int
of the game in the season, where 1 is the first game of the season.
- property game_duration
MM’.
- Type
Returns a
string
of the game’s total duration in the format ‘H
- property game_number_for_day
Returns an
int
denoting which game is played for the team during the given day. Default value is 1 where a team plays only one game during the day, but can be higher for double headers, etc. For example, if a team has a double header one day, the first game of the day will return 1 while the second game will return 2.
- property games_behind
Returns a
float
of the number of games behind the leader the team is. 0.0 indicates the team is tied for first. Negative numbers indicate the number of games a team is ahead of the second place team.
- property innings
Returns an
int
of the total number of innings that were played.
- property location
Returns a
string
constant to indicate whether the game was played at home or away.
- property loser
Returns a
string
of the name of the losing pitcher.
- property opponent_abbr
Returns a
string
of the opponent’s 3-letter abbreviation, such as ‘NYY’ for the New York Yankees.
- property rank
Returns an
int
of the team’s rank in the league with 1 being the best team.
- property record
Returns a
string
of the team’s record in the format ‘W-L’.
- property result
Returns a
string
constant to indicate whether the team won or lost.
- property runs_allowed
Returns an
int
of the total number of runs that the team allowed.
- property runs_scored
Returns an
int
of the total number of runs that were scored by the team.
- property save
Returns a
string
of the name of the pitcher credited with the save if applicable. If no saves, returns None.
- property streak
Returns a
string
of the team’s winning/losing streak at the conclusion of the requested game. A winning streak is denoted by a number of ‘+’ signs for the number of consecutive wins and a losing streak is denoted by a ‘-’ sign.
- property winner
Returns a
string
of the name of the winning pitcher.
- class sportsipy.mlb.schedule.Schedule(abbreviation, year=None)[source]
Bases:
object
An object of the given team’s schedule.
Generates a team’s schedule for the season including wins, losses, and scores if applicable.
- Parameters
abbreviation (string) – A team’s short name, such as ‘HOU’ for the Houston Astros.
year (string (optional)) – The requested year to pull stats from.
- property dataframe
Returns a pandas DataFrame where each row is a representation of the Game class. Rows are indexed by the boxscore string.
- property dataframe_extended
Returns a pandas DataFrame where each row is a representation of the Boxscore class for every game in the schedule. Rows are indexed by the boxscore string. This property provides much richer context for the selected game, but takes longer to process compared to the lighter ‘dataframe’ property.
Teams
The Teams module exposes information for all MLB teams including the team name and abbreviation, the number of games they won during the season, the total number of bases they’ve stolen, and much more.
from sportsipy.mlb.teams import Teams
teams = Teams()
for team in teams:
print(team.name) # Prints the team's name
print(team.batting_average) # Prints the team's season batting average
A team can also be requested directly by calling the Team
class which
returns a Team instance identical to the one in each element in the loop above.
To request a specific team, use the 3-letter abbreviation for the team while
calling Team class.
from sportsipy.mlb.teams import Team
houston = Team('HOU')
Each Team instance contains a link to the Schedule
class which enables easy
iteration over all games for a particular team. A Pandas DataFrame can also be
queried to easily grab all stats for all games.
from sportsipy.mlb.teams import Teams
teams = Teams()
for team in teams:
schedule = team.schedule # Returns a Schedule instance for each team
# Returns a Pandas DataFrame of all metrics for all game Boxscores for
# a season.
df = team.schedule.dataframe_extended
Lastly, each Team instance also contains a link to the Roster
class which
enables players from the team to be easily queried. Each Roster instance
contains detailed stats and information for each player on the team.
from sportsipy.mlb.teams import Teams
for team in Teams():
roster = team.roster # Gets each team's roster
for player in roster.players:
print(player.name) # Prints each players name on the roster
- class sportsipy.mlb.teams.Team(team_name=None, team_data=None, rank=None, year=None, standings_file=None, teams_file=None)[source]
Bases:
object
An object containing all of a team’s season information.
Finds and parses all team stat information and identifiers, such as rank, name, and abbreviation, and sets them as properties which can be directly read from for easy reference.
If calling directly, the team’s abbreviation needs to be passed. Otherwise, the Teams class will handle all arguments.
- Parameters
team_name (string (optional)) – The name of the team to pull if being called directly.
team_data (string (optional)) – A string containing all of the rows of stats for a given team. If multiple tables are being referenced, this will be comprised of multiple rows in a single string. Is only used when called directly from the Teams class.
rank (int (optional)) – A team’s position in the league based on the number of points they obtained during the season. Is only used when called directly from the Teams class.
year (string (optional)) – The requested year to pull stats from.
standings_file (string (optional)) – Optionally specify the filename of a local file to use to pull data instead of downloading from sports-reference.com. This file should be of the Standings page for the designated year.
teams_file (string (optional)) – Optionally specify the filename of a local file to use to pull data instead of downloading from sports-reference.com. This file should be of the League page for the designated year.
- property abbreviation
Returns a
string
of the team’s abbreviation, such as ‘HOU’ for the Houston Astros.
- property at_bats
Returns an
int
of the total number of at bats for the team.
- property average_batter_age
Returns a
float
of the average batter age weighted by their number of at bats plus the number of games participated in.
- property average_pitcher_age
Returns a
float
of the average pitcher age weighted by the number of games started, followed by the number of games played and saves.
- property away_losses
Returns an
int
of the number of away losses during the season.
- property away_record
Returns a
string
of the team’s away record. Record is in the format ‘W-L’.
- property away_wins
Returns an
int
of the number of away wins during the season.
- property balks
Returns an
int
of the total number of times a pitcher has balked.
- property bases_on_balls
Returns an
int
of the number of bases on walks.
- property bases_on_walks_given
Returns an
int
of the total number of bases from walks given up by a team during the season.
- property bases_on_walks_given_per_nine_innings
Returns a
float
of the average number of walks conceded per nine innings.
- property batters_faced
Returns an
int
of the total number of batters all pitchers have faced during a season.
- property batting_average
Returns a
float
of the batting average for the team. Percentage ranges from 0-1.
- property complete_game_shutouts
Returns an
int
of the total number of complete games where the opponent scored zero runs.
- property complete_games
Returns an
int
of the total number of complete games a team has accumulated during the season.
- property dataframe
Returns a pandas DataFrame containing all other class properties and values. The index for the DataFrame is the string abbreviation of the team, such as ‘HOU’.
- property doubles
Returns an
int
of the total number of doubles hit by the team.
- property earned_runs_against
Returns a
float
of the average number of earned runs against for a team.
- property earned_runs_against_plus
Returns an
int
of the team’s average earned runs against, adjusted for the home ballpark.
- property extra_inning_losses
Returns an
int
of the number of losses the team has when the game has gone to extra innings.
- property extra_inning_record
Returns a
string
of the team’s record when the game has gone to extra innings. Record is in the format ‘W-L’.
- property extra_inning_wins
Returns an
int
of the number of wins the team has when the game has gone to extra innings.
- property fielding_independent_pitching
Returns a
float
of the team’s effectiveness at preventing home runs, walks, batters being hit by pitches, and strikeouts.
- property games
Returns an
int
of the number of games the team has played during the season.
- property games_finished
Returns an
int
of the number of games finished which is equivalent to the number of games played minus the number of complete games during the season.
- property grounded_into_double_plays
Returns an
int
of the total number double plays grounded into by the team.
- property hit_pitcher
Returns an
int
of the total number of times a pitcher has hit an opposing batter.
- property hits
Returns an
int
of the total number of hits during the season.
- property hits_allowed
Returns an
int
of the total number of hits allowed during the season.
- property hits_per_nine_innings
Returns a
float
of the average number of hits per nine innings by the opponent.
- property home_losses
Returns an
int
of the number of losses at home during the season.
- property home_record
Returns a
string
of the team’s home record. Record is in the format ‘W-L’.
- property home_runs
Returns an
int
of the total number of home runs hit by the team.
- property home_runs_against
Returns an
int
of the total number of home runs given up during the season.
- property home_runs_per_nine_innings
Returns a
float
of the average number of home runs per nine innings by the opponent.
- property home_wins
Returns an
int
of the number of wins at home during the season.
- property innings_pitched
Returns a
float
of the total number of innings pitched by a team during the season.
- property intentional_bases_on_balls
Returns an
int
of the total number of times a player took a base from an intentional walk.
- property interleague_record
Returns a
string
of the team’s interleague record. Record is in the format ‘W-L’.
- property last_ten_games_record
Returns a
string
of the team’s record over the last ten games. Record is in the format ‘W-L’.
- property last_thirty_games_record
Returns a
string
of the team’s record over the last thirty games. Record is in the format ‘W-L’.
- property last_twenty_games_record
Returns a
string
of the team’s record over the last twenty games. Record is in the format ‘W-L’.
- property league
Returns a
string
of the two letter abbreviation of the league, such as ‘AL’ for the American League.
- property losses
Returns an
int
of the total number of games the team lost during the season.
- property losses_last_ten_games
Returns an
int
of the number of losses in the last 10 games.
- property losses_last_thirty_games
Returns an
int
of the number of losses in the last 30 games.
- property losses_last_twenty_games
Returns an
int
of the number of losses in the last 20 games.
- property losses_vs_left_handed_pitchers
Returns an
int
of number of losses against left-handed pitchers.
- property losses_vs_right_handed_pitchers
Returns an
int
of the number of losses against right-handed pitchers.
- property losses_vs_teams_over_500
Returns an
int
of the number of losses against teams over 500.
- property losses_vs_teams_under_500
Returns an
int
of the number of losses against teams under 500.
- property luck
Returns an
int
of the difference between the current wins and losses compared to the pythagorean wins and losses.
- property name
Returns a
string
of the team’s full name, such as ‘Houston Astros’.
- property number_of_pitchers
Returns an
int
of the total number of pitchers used during a season.
- property number_players_used
Returns an
int
of the number of different players used during the season.
- property on_base_percentage
Returns a
float
of the percentage of at bats that result in a player taking a base. Percentage ranges from 0-1.
- property on_base_plus_slugging_percentage
Returns a
float
of the sum of the on base percentage plus the slugging percentage.
- property on_base_plus_slugging_percentage_plus
Returns an
int
of the on base percentage plus the slugging percentage, adjusted to the team’s home ballpark.
- property opposing_runners_left_on_base
Returns an
int
of the total number of opponents a team has left on bases at the end of an inning.
- property plate_appearances
Returns an
int
of the total number of plate appearances for the team.
- property pythagorean_win_loss
Returns a
string
of the team’s expected win-loss record based on the runs scored and allowed. Record is in the format ‘W-L’.
- property rank
Returns an
int
of the team’s rank based on their win percentage.
- property record_vs_left_handed_pitchers
Returns a
string
of the team’s record against left-handed pitchers. Record is in the format ‘W-L’.
- property record_vs_right_handed_pitchers
Returns a
string
of the team’s record against right-handed pitchers. Record is in the format ‘W-L’.
- property record_vs_teams_over_500
Returns a
string
of the team’s record against teams with a win percentage over 500. Record is in the format ‘W-L’.
- property record_vs_teams_under_500
Returns a
string
of the team’s record against teams with a win percentage under 500. Record is in the format ‘W-L’.
- property roster
Returns an instance of the Roster class containing all players for the team during the season with all career stats.
- property run_difference
Returns a
float
of the difference between the number of runs scored and the number of runs given up per game. Positive numbers indicate the team scores more per game than they are scored on.
- property runners_left_on_base
Returns an
int
of the total number of runners left on base at the end of an inning.
- property runs
Returns a
float
of the average number of runs scored per game by the team.
- property runs_against
Returns a
float
of the average number of runs scored per game by the opponent.
- property runs_allowed_per_game
Returns a
float
of the average number of runs a team has allowed per game.
- property runs_batted_in
Returns an
int
of the total number of runs batted in by the team.
- property sacrifice_flies
Returns an
int
of the total number of sacrifice flies the team made during the season.
- property sacrifice_hits
Returns an
int
of the total number of sacrifice hits the team made during the season.
- property saves
Returns an
int
of the total number of saves a team has accumulated during the season.
- property schedule
Returns an instance of the Schedule class containing the team’s complete schedule for the season.
- property shutouts
Returns an
int
of the total number of shutouts a team has accumulated during the season.
- property simple_rating_system
Returns a
float
of the average number of runs per game a team scores compared to average.
- property single_run_losses
Returns an
int
of the number of losses the team has when only one run is scored.
- property single_run_record
Returns a
string
of the team’s record when only one run is scored. Record is in the format ‘W-L’.
- property single_run_wins
Returns an
int
of the number of wins the team has when only one run is scored.
- property slugging_percentage
Returns a
float
of the ratio of total bases gained per at bat.
- property stolen_bases
Returns an
int
of the total number of bases stolen by the team.
- property streak
Returns a
string
of the team’s current winning or losing streak, such as ‘W 3’ for a team on a 3-game winning streak.
- property strength_of_schedule
Returns a
float
denoting a team’s strength of schedule, based on runs scores and conceded. Higher values result in more challenging schedules while 0.0 is an average schedule.
- property strikeouts
Returns an
int
of the total number of times a team has struck out an opponent.
- property strikeouts_per_base_on_balls
Returns a
float
of the average number of strikeouts per walk thrown by a team.
- property strikeouts_per_nine_innings
Returns a
float
of the average number of strikeouts a team throws per nine innings.
- property times_caught_stealing
Returns an
int
of the number of times a player was caught stealing.
- property times_hit_by_pitch
Returns an
int
of the total number of times a batter was hit by an opponent’s pitch.
- property times_struck_out
Returns an
int
of the total number of times the team struck out.
- property total_bases
Returns an
int
of the total number of bases a team has gained during the season.
- property total_runs
Returns an
int
of the total number of runs scored during the season.
- property triples
Returns an
int
of the total number of tripes hit by the team.
- property whip
Returns a
float
of the average number of walks plus hits by the opponent per inning.
- property wild_pitches
Returns an
int
of the total number of wild pitches thrown by a team during a season.
- property win_percentage
Returns a
float
of the number of wins divided by the number of games played during the season. Percentage ranges from 0-1.
- property wins
Returns an
int
of the total number of games the team won during the season.
- property wins_last_ten_games
Returns an
int
of the number of wins in the last 10 games.
- property wins_last_thirty_games
Returns an
int
of the number of wins in the last 30 games.
- property wins_last_twenty_games
Returns an
int
of the number of wins in the last 20 games.
- property wins_vs_left_handed_pitchers
Returns an
int
of number of wins against left-handed pitchers.
- property wins_vs_right_handed_pitchers
Returns an
int
of the number of wins against right-handed pitchers.
- property wins_vs_teams_over_500
Returns an
int
of the number of wins against teams over 500.
- property wins_vs_teams_under_500
Returns an
int
of the number of wins against teams under 500.
- class sportsipy.mlb.teams.Teams(year=None, standings_file=None, teams_file=None)[source]
Bases:
object
A list of all MLB teams and their stats in a given year.
Finds and retrieves a list of all MLB teams from www.baseball-reference.com and creates a Team instance for every team that participated in the league in a given year. The Team class comprises a list of all major stats and a few identifiers for the requested season.
- Parameters
year (string (optional)) – The requested year to pull stats from.
standings_file (string (optional)) – Optionally specify the filename of a local file to use to pull data instead of downloading from sports-reference.com. This file should be of the Standings page for the designated year.
teams_file (string (optional)) – Optionally specify the filename of a local file to use to pull data instead of downloading from sports-reference.com. This file should be of the League page for the designated year.
- property dataframes
Returns a pandas DataFrame where each row is a representation of the Team class. Rows are indexed by the team abbreviation.