Package: pybbda
pybbdapybbdais a package for
Python Baseball Data and Analysis.datapybbdaaims to provide a uniform framework for
accessing baseball data from various sources.
The data are exposed aspandasDataFramesThe data sources it currently supports are:Lahman dataBaseball Reference WARFangraphs leaderboards and park factorsRetrosheet event dataStatcast pitch-by-pitch dataanalysispybbdaalso provides analysis tools.It currently supports:Marcel projectionsBatted ball trajectoriesRun expectancy via Markov chainsThe following are planned for a future release:Simulationsand more...!InstallationThis package is available on PyPI, so you can install it withpip,$pipinstall-UpybbdaOr you can install the latest master branch
directly from the github repo usingpip,$pipinstallgit+https://github.com/bdilday/pybbda.gitor download the source,$gitclonegit@github.com:bdilday/pybbda.git
$cdpybbda
$pipinstall.RequirementsThis package explicitly
supportsPython 3.6andPython 3.7. It aims
to supportPython 3.8but this is not guaranteed.
It explicitlydoes notsupport any versions
prior toPython 3.6, includingPython 2.7.Installing dataThis package ships without any data. Instead it provides tools
to fetch and store data from a variety of sources.To install data you can use theupdatetool in thepybbda.data.toolssub-module.Example,$python-mpybbda.data.tools.update-h
usage:update.py[-h][--data-rootDATA_ROOT]--data-source{Lahman,BaseballReference,Fangraphs,retrosheet,all}[--make-dirs][--overwrite][--min-yearMIN_YEAR][--max-yearMAX_YEAR][--num-threadsNUM_THREADS]optionalarguments:-h,--helpshowthishelpmessageandexit--data-rootDATA_ROOTRootdirectoryfordatastorage--data-source{Lahman,BaseballReference,Fangraphs,retrosheet,all}Updatesource--make-dirsMakerootdirifdoesnotexist--overwriteOverwritefilesiftheyexist--min-yearMIN_YEARMinyeartodownload--max-yearMAX_YEARMaxyeartodownload--num-threadsNUM_THREADSNumberofthreadstousefordownloadsThe data will be downloaded to--data-root, which defaults to thePYBBDA_DATA_ROOTDetailed instructions areprovided in the documentationExample UsageAfter installing some or all of the data, you can start using the
package.Following is an example of accessing Lahman data.
Moreexamples are included in the documentationLahman data>>>frompybbda.dataimportLahmanData>>>lahman_data=LahmanData()>>>batting_df=lahman_data.battingINFO:pybbda.data.sources.lahman.data:data:searchingforfile/home/bdilday/.pybbda/data/Lahman/Batting.csv>>>batting_df.head()playerIDyearIDstintteamIDlgIDGABRH2B3BHRRBISBCSBBSOIBBHBPSHSFGIDP0abercda0118711TRONaN14000000.00.00.000.0NaNNaNNaNNaN0.01addybo0118711RC1NaN25118303260013.08.01.040.0NaNNaNNaNNaN0.02allisar0118711CL1NaN29137284045019.03.01.025.0NaNNaNNaNNaN1.03allisdo0118711WS3NaN271332844102227.01.01.002.0NaNNaNNaNNaN0.04ansonca0118711RC1NaN251202939113016.06.02.021.0NaNNaNNaNNaN0.0>>>batting_df.groupby("playerID").HR.sum().sort_values(ascending=False)playerIDbondsba01762aaronha01755ruthba01714rodrial01696mayswi01660...mcconra010mccolal010mccluse010mcclula010aardsda010Name:HR,Length:19689,dtype:int64CLI toolsRun expectanciesThere is a cli tool for computing run expectancies from
Markov chains.$python-mpybbda.analysis.run_expectancy.markov.cli--helpThis Markov chain uses a lineup of
9 batters instead of assuming each batter has the same characteristics.
You can also assign running probabilities, although they apply to
all batters equally.You can assign batting-event probabilities using a sequence of
probabilities, or by referencing a player-season with the
format{playerID}_{season}, where playerID is the
Lahman ID and season is a 4-digit year. For example, to
refer to Rickey Henderson's 1982 season, usehenderi01_1982.The lineup is assigned by giving the lineup slot followed by either
5 probabilities, or a player-season id. The lineup-slot 0 is a code
to assign all nine batters to this value. Any other specific slots
will be filled in as noted.The number of outs to model is 3 by default. It can be changed by setting the
environment variablePYBBDA_MAX_OUTS.Example: Use a default set of probabilities for all 9 slots with no taking extra bases$python-mpybbda.analysis.run_expectancy.markov.cli-b00.080.150.050.0050.03--running-probs0000meanscoreper27outs=3.5227
std.scoreper27outs=2.8009Example: Use a default set of probabilities for all 9 slots with default probabilities for taking extra bases$python-mpybbda.analysis.run_expectancy.markov.cli-b00.080.150.050.0050.03
meanscoreper27outs=4.2242
std.scoreper27outs=3.0161Example: Use a default set of probabilities for all 9 slots but let
Rickey Henderson 1982 bat leadoff (using 27 outs, instead of 3)$PYBBDA_MAX_OUTS=27python-mpybbda.analysis.run_expectancy.markov.cli-b00.080.150.050.0050.03-i1henderi01_1982
WARNING:pybbda:__init__:EnvironmentvariablePYBBDA_DATA_ROOTisnotset,defaultingto/home/bdilday/github/pybbda/pybbda/data/assets
INFO:pybbda.data.sources.lahman.data:data:searchingforfile/home/bdilday/github/pybbda/pybbda/data/assets/Lahman/Batting.csv
meanscoreper27outs=4.3628
std.scoreper27outs=3.0999Example: Use a default set of probabilities for all 9 slots but let
Rickey Henderson 1982 bat leadoff and Babe Ruth 1927 bat clean-up (using 27 outs, instead of 3)$PYBBDA_MAX_OUTS=27python-mpybbda.analysis.run_expectancy.markov.cli-b00.080.150.050.0050.03-i1henderi01_1982-i4ruthba01_1927WARNING:pybbda:__init__:EnvironmentvariablePYBBDA_DATA_ROOTisnotset,defaultingto/home/bdilday/github/pybbda/pybbda/data/assets
INFO:pybbda.data.sources.lahman.data:data:searchingforfile/home/bdilday/github/pybbda/pybbda/data/assets/Lahman/Batting.csv
meanscoreper27outs=5.1420
std.scoreper27outs=3.3996ContributingContributions from the community are welcome.
See thecontributing guide.LicenseGPLv2