Skip to main content

pyretrosheet

PyPI version Coverage

pyretrosheet is under active development and is not feature complete.

Load, analyze, and enrich retrosheet.org MLB data using Python representations.

Retrosheet provides play-by-play and other miscellaneous MLB data (at the time of writing, includes all play-by-play data for all AL and NL seasons from 1919 to 2022).

pyretrosheet provides functionality for:

  • downloading Retrosheet play-by-play data
  • parsing and loading play-by-play data into Python objects to make the data easier to understand and analyze
  • enriching data to include player and summary statistics not encoded directly by Retrosheet

pyretrosheet does not provide functionality for:

  • downloading/using MLB data from sources other than Retrosheet

See Retrosheet Data Resources for other tools that parse Retrosheet event files. At the time of writing, these resources focus on loading/dumping to other data formats like CSV and SQL databases.

Usage

pip install pyretrosheet

Load Games

By default, data downloaded from retrosheet.org is stored at ~/.pyretrosheet/data/, but can be overriden via the data_dir argument.

import pyretrosheet

games = pyretrosheet.load_games(year=2022)

print(games[0])
"""
Game(
  id=GameID(home_team_id='SFN', date=datetime.date(2022, 4, 8), game_number=0, raw='id,SFN202204080'),
  home_team_id=SFN,
  visiting_team_id=MIA,
  num_chronological_events=150,
  earned_runs={'bleir001': 1, 'alcas001': 2, 'bassa001': 1, 'benda001': 1, 'webbl001': 1, 'dovac001': 3, 'leond003': 1},
)
"""

TODO: Add more examples

Data Availability

Retrosheet Event File Coverage

Retrosheet's Event File Spec defines the encoding for event files (play-by-play game data). The spec (as of 11/30/2023) can also be found at docs/event_file_spec.txt.

There is a wide amount of data encoded into these files and this package does not cover all encodings.

Contributions are welcome for any encodings not covered!

Covered

  • Loading all games from a given event file
  • Record types
    • id
    • info
    • start
    • sub
    • play
    • data
    • com

Not Covered

  • Record types

    • play's pitching encoding
    • radj
    • badj
    • padj
    • ladj
    • presadj
  • Miscellaneous data

    • replays
    • ejections
    • umpire changes
    • protests
    • suspensions

Enriched

pyretrosheet provides enriched Retrosheet data to provide:

  • TODO

Contributing

Makefile targets

help: Show this help.
setup: Install the package and dev dependencies into a virtualenv.
test:  Run pytest on the tests dir.
test_all_data:  Run pytest on all Retrosheet data.
format: Run black and isort on package and tests dirs.
lint:  Run ruff and mypy on package files.
coverage:  Run test coverage and update coverage badge
bump_version:  Increment patch version references in the project
publish_to_testpypi:  Publish the package to test.pypi.org.
publish_to_pypi:  Publish the package to pypi.org.

Todo

Non-Trivial

  • ReadTheDocs
  • Verify enriched data with alternative sources like Baseball Reference
  • Determine top-level interface for querying data
  • Implement index of game files to easily lookup games for:
    • a specific team within a year
    • a specific game
  • Stats
    • Hits (H)
    • Walks (W)
    • Hit By Pitches (HBP)
    • Sacrifice Flys (SF)
    • At Bats (AB)
    • Singles (S)
    • Doubles (D)
    • Triples (T)
    • Home Runs (HR)
    • Composite
      • Batting Average (BA)
      • Slugging Percentage (SP)
      • On Base Percentage (OBP)
    • Difficult and Needs Lots of Validation
      • Runs (R)
      • Runs Batted In (RBI)
  • Aggregate stats
    • Mean, Median, Std. Dev, Min, Max

Trivial

  • Parse out 'info' fields into pyretrosheet.models.Game properties
  • Encoding pitches from play data
  • Improve error handling for inability to retrieve Retrosheet data
  • Improve README Usage examples
  • Add CONTRIBUTING.md
  • Add interface to load stats

Retrosheet Notice

The information used here was obtained free of charge from and is copyrighted by Retrosheet. Interested parties may contact Retrosheet at 20 Sunset Rd., Newark, DE 19711.

Credits

  • Project skeleton generated via cookiecutter https://github.com/rozelie/Python-Project-Cookiecutter
  • Thank you Retrosheet team for making your data free and publicly available!

Release files for pyretrosheet 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyretrosheet 0.0.10
File Size Uploaded
pyretrosheet-0.0.10.tar.gz 22.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyretrosheet 0.0.10
File Interpreter ABI Platform
pyretrosheet-0.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 46.7 kB

Release files / pyretrosheet-0.0.10.tar.gz

Download URL pyretrosheet-0.0.10.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b233152c72adc7046e18755503fe9bbcf5c7b3113320013353088739b0e32110
BLAKE2b-256 checksum
How to use checksums
f05e2f3d90dbf77ac5f8c564ba019110073badc09bf7fbb5aaf91562cc12b94d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.3

Release files / pyretrosheet-0.0.10-py3-none-any.whl

Download URL pyretrosheet-0.0.10-py3-none-any.whl
Size 24.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd7797a217efaaa0f5cec434f9c4e184817687d7cf3f3b560cb28b7d0dcfb4bd
BLAKE2b-256 checksum
How to use checksums
105381a95e8cda7ae5661875a8d0c18ebeed141d58f17a4da1ee1e09623c9d03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page