pybaseball

repository·master·Indexed 23 days ago

https://github.com/jldbc/pybaseball

A Python package for baseball data analysis that scrapes data from Baseball Reference, Baseball Savant, and FanGraphs. It provides access to pitch-level Statcast metrics, aggregated season statistics, team standings, amateur draft results, and player ID lookups via MLBAM IDs. The library includes built-in caching support with CSV and Parquet storage options to optimize repeated data requests.

Tokens
21.9K
Snippets
69
Records
106
Agent score
80%

What's inside pybaseball

  1. How pybaseball caching logic works

    master

    The cache operates at the function parameter level. A cache hit occurs only when a function is called with the exact same parameters as a previous call.

    Note on subsetting: The cache does not support subsetting. For example, if you call pybaseball.batting_leaders(2000, 2020), a subsequent call to pybaseball.batting_leaders(2010, 2015) will result in a new download/cache entry rather than reusing the existing data, even though the required data is already present in the cache.

  2. Enable or disable caching in pybaseball

    master

    Caching is disabled by default. You can enable or disable it using the cache module. When enabled, pybaseball will store downloaded data to a local directory to speed up subsequent requests.

    from pybaseball import cache
    
    # Enable the cache
    cache.enable()
    
    # Disable the cache
    cache.disable()
  3. Increase Retrosheet data download rate limits via GH_TOKEN

    master

    When pulling Retrosheet data, pybaseball checks requests against the Chadwick Bureau GitHub repository. Unauthenticated requests are limited to 60 queries per hour by the GitHub API. To increase this limit to 5000 queries per hour, you must use a GitHub Personal Access Token.

    1. Create a Personal Access Token on GitHub.
    2. Set the environment variable GH_TOKEN with your token value:
      • Mac/Linux: Add GH_TOKEN=<your_token> to your .bashrc file.
      • Windows: Add GH_TOKEN to your environment variables via the Start menu.

    Providing this token is optional but recommended as it provides more instructive errors by checking against existing files before attempting a download.

    GH_TOKEN=<your_token>
  4. Install pybaseball

    master

    You can install pybaseball via pip or directly from the GitHub repository to get the most up-to-date version.

    Via pip:

    pip install pybaseball

    From source:

    git clone https://github.com/jldbc/pybaseball
    cd pybaseball
    pip install -e .
    pip install pybaseball
  5. Configure cache storage type (CSV or Parquet)

    master

    The cache supports two storage mechanisms: 'CSV' and 'Parquet'. The default storage type is Parquet. To change the storage mechanism, update cache.config.cache_type and call cache.config.save() to persist the setting.

    from pybaseball import cache
    
    cache.enable()
    cache.config.cache_type='csv'
    cache.config.save()
  6. Configure the cache directory location

    master
    By default, pybaseball caches data to the .pybaseball/cache folder in the user's home directory. You can override this location by setting the PYBASEBALL_CACHE environment variable to your desired directory path.
  7. Enable and manage data caching

    master

    To speed up repeated data requests, you can enable a local data cache. Caching is disabled by default.

    Enable Cache:

    from pybaseball import cache
    cache.enable()

    Clear Cache: If you are receiving unexpected results (e.g., empty datasets for future dates), clear the cache:

    from pybaseball import cache
    cache.purge()
    from pybaseball import cache
    cache.enable()
  8. Troubleshoot BrokenProcessPool errors

    master

    If you encounter a concurrent.futures.process.BrokenProcessPool error, it is likely due to the system using spawn-based processes (common on Windows and macOS). To fix this, wrap your library calls within a if __name__ == '__main__': block.

    from pybaseball import statcast
    
    if __name__ == '__main__':
        stats = statcast()
    if __name__ == '__main__':
        stats = statcast()
  9. Filter pitching stats by qualification

    master

    When using pitching_stats(), you can use the qual argument to filter for players who meet a specific threshold of plate appearances. This is useful for removing noise from players with very small sample sizes.

    Example: Retrieving data for only players who have pitched 50+ innings (or met a specific plate appearance threshold) in a given year.

    from pybaseball import pitching_stats
    
    # retrieve data on only players who have pitched 50+ innings this year
    data = pitching_stats(2017, qual=50)
  10. Find an MLBAM player ID using playerid_lookup

    master

    To use statcast_pitcher_spin, you must first find the pitcher's MLBAM ID. You can use the playerid_lookup function to search for players by name.

    from pybaseball import playerid_lookup
    
    # find Chris Sale's player id (mlbam_key)
    playerid_lookup('darvish','yu')