nflreadpy

repository·main·Indexed 41 days ago

https://github.com/nflverse/nflreadpy

A Python package for downloading NFL data from nflverse repositories. A port of the R package nflreadr, it provides fast data access using Polars DataFrames with built-in caching and progress tracking. It includes functions to load play-by-play data, player and team stats, schedules, rosters, and advanced metrics from sources like nextgenstats.nfl.com and ftnfantasy.com.

Tokens
8.6K
Snippets
34
Records
39
Agent score
53%

What's inside nflreadpy

  1. Install nflreadpy

    main

    You can install the latest release from PyPI using uv (recommended) or pip. To install the latest development version directly from GitHub, use the git URL.

    # Latest release
    uv add nflreadpy
    
    # Development version
    uv add nflreadpy@git+https://github.com/nflverse/nflreadpy

    Using pip

    # Latest release
    pip install nflreadpy
    
    # Development version
    pip install nflreadpy@git+https://github.com/nflverse/nflreadpy
    uv add nflreadpy
  2. Basic usage of nflreadpy

    main

    Import nflreadpy as nfl to access data loading functions. Note that nflreadpy returns Polars DataFrames by default. If your workflow requires pandas, you must explicitly convert them using .to_pandas().

    import nflreadpy as nfl
    
    # Load current season play-by-play data
    pbp = nfl.load_pbp()
    
    # Load player game-level stats for multiple seasons
    player_stats = nfl.load_player_stats([2022, 2023])
    
    # Load all available team level stats
    team_stats = nfl.load_team_stats(seasons=True)
    
    # Convert Polars DataFrame to pandas
    pbp_pandas = pbp.to_pandas()
    import nflreadpy as nfl
    
    pbp = nfl.load_pbp()
    player_stats = nfl.load_player_stats([2022, 2023])
    team_stats = nfl.load_team_stats(seasons=True)
    pbp_pandas = pbp.to_pandas()
  3. Configure nflreadpy via environment variables

    main

    You can configure the behavior of nflreadpy using the following environment variables:

    VariableDescription
    NFLREADPY_CACHECache mode ("memory", "filesystem", or "off")
    NFLREADPY_CACHE_DIRDirectory path for filesystem cache
    NFLREADPY_CACHE_DURATIONCache duration in seconds
    NFLREADPY_VERBOSEEnable verbose output (true/false)
    NFLREADPY_TIMEOUTHTTP request timeout in seconds
    NFLREADPY_USER_AGENTCustom user agent string
    export NFLREADPY_CACHE='memory'
    export NFLREADPY_CACHE_DIR='~/my_cache_dir'
    export NFLREADPY_CACHE_DURATION=86400
    export NFLREADPY_VERBOSE='False'
    export NFLREADPY_TIMEOUT=30
    export NFLREADPY_USER_AGENT='nflreadpy/v0.1.1'
  4. How caching works in nflreadpy

    main

    Caching in nflreadpy is designed to be transparent. Data functions (like load_pbp) automatically check the cache before attempting a network request and store the results after a successful download.

    Depending on your configuration, the system supports three modes:

    1. MEMORY: Fast repeated access within the same session using an internal dictionary.
    2. FILESYSTEM: Persistent storage across sessions using Parquet files in a configurable directory.
    3. OFF: No caching is performed.

    Cache expiration is handled automatically based on a configurable cache_duration. The system checks the timestamp of memory items or the modification time of filesystem files to determine if they are still valid.

    import nflreadpy as nfl
    
    # Data is automatically cached on first call
    pbp = nfl.load_pbp([2023])
    
    # Subsequent calls use cached data and are much faster
    pbp_again = nfl.load_pbp([2023])
  5. Get the current NFL week with get_current_week()

    main

    Use nflreadpy.get_current_week() to retrieve the integer representing the current NFL week. This is useful for filtering data or identifying the most recent games in a dataset.

    import nflreadpy
    
    current_week = nflreadpy.get_current_week()
    print(f"The current week is: {current_week}")
  6. Configure nflreadpy programmatically

    main

    Use update_config from nflreadpy.config to set configuration settings within your Python code.

    from nflreadpy.config import update_config
    
    update_config(
        cache_mode="memory",
        cache_dir='~/my_cache_dir',
        cache_duration=86400,
        verbose=False,
        timeout=30,
        user_agent='nflreadpy/v0.1.1'
    )
  7. Manage nflreadpy configuration

    main

    The nflreadpy configuration can be managed using the nflreadpy.config module. You can retrieve the current configuration, update settings, or reset the configuration to its default state using the following functions:

    • get_config(): Returns the current configuration object.
    • update_config(new_config): Updates the global configuration with the provided settings.
    • reset_config(): Resets the configuration to its original default values.
    • NflreadpyConfig: The configuration class used to define and hold settings.
  8. Get the current NFL season with get_current_season()

    main

    Use nflreadpy.get_current_season() to retrieve the integer representing the current NFL season. This is useful for dynamically loading data for the active year without hardcoding season values.

    import nflreadpy
    
    current_season = nflreadpy.get_current_season()
    print(f"The current season is: {current_season}")
  9. Clear the nflreadpy cache

    main

    Use nflreadpy.clear_cache() to remove all cached data files from your local system. This is useful if you suspect your local cache is corrupted or if you want to force the library to re-download the latest data from the source instead of using existing local files.

    import nflreadpy
    
    nflreadpy.clear_cache()
  10. Available data loading functions in nflreadpy

    main

    The nflreadpy library provides a suite of functions to load various NFL datasets. These functions return data in Polars DataFrames. The available loading functions are:

    • load_pbp: Play-by-play data
    • load_player_stats: Player game-level stats
    • load_team_stats: Team-level stats
    • load_schedules: NFL schedules
    • load_teams: Team information
    • load_players: Player information
    • load_rosters: Season-level rosters
    • load_rosters_weekly: Weekly rosters
    • load_snap_counts: Player snap counts
    • load_nextgen_stats: Next Gen Stats
    • load_ftn_charting: FTN charting data
    • load_participation: Participation data
    • load_draft_picks: NFL draft picks
    • load_injuries: Injury reports
    • load_contracts: Player contracts
    • load_officials: NFL officials data
    • load_combine: NFL Combine data
    • load_depth_charts: Team depth charts
    • load_trades: NFL trades
    • load_pfr_advstats: Pro Football Reference advanced stats
    • load_ff_playerids: Fantasy Football player IDs
    • load_ff_rankings: Fantasy Football rankings
    • load_ff_opportunity: Fantasy Football opportunity data
    nflreadpy.load_pbp()
    nflreadpy.load_player_stats()
    nflreadpy.load_team_stats()
    nflreadpy.load_schedules()
    nflreadpy.load_teams()
    nflreadpy.load_players()
    nflreadpy.load_rosters()
    nflreadpy.load_rosters_weekly()
    nflreadpy.load_snap_counts()
    nflreadpy.load_nextgen_stats()
    nflreadpy.load_ftn_charting()
    nflreadpy.load_participation()
    nflreadpy.load_draft_picks()
    nflreadpy.load_injuries()
    nflreadpy.load_contracts()
    nflreadpy.load_officials()
    nflreadpy.load_combine()
    nflreadpy.load_depth_charts()
    nflreadpy.load_trades()
    nflreadpy.load_pfr_advstats()
    nflreadpy.load_ff_playerids()
    nflreadpy.load_ff_rankings()
    nflreadpy.load_ff_opportunity()
  11. Reference of core loading functions

    main

    The following functions are available in nflreadpy to load various NFL datasets. Most return Polars DataFrames.

    FunctionDescription
    load_pbp()play-by-play data
    load_player_stats()player game or season statistics
    load_team_stats()team game or season statistics
    load_schedules()game schedules and results
    load_players()player information
    load_rosters()team rosters
    load_rosters_weekly()team rosters by season-week
    load_snap_counts()snap counts
    load_nextgen_stats()advanced stats from nextgenstats.nfl.com
    load_ftn_charting()charted stats from ftnfantasy.com/data
    load_participation()participation data (historical)
    load_draft_picks()nfl draft picks
    load_injuries()injury statuses and practice participation
    load_contracts()historical contract data from OTC
    load_officials()officials for each game
    load_combine()nfl combine results
    load_depth_charts()depth charts
    load_trades()trades
    load_ff_playerids()ffverse/dynastyprocess player ids
    load_ff_rankings()fantasypros rankings
    load_ff_opportunity()expected yards, touchdowns, and fantasy points
  12. Reference of utility functions

    main

    Use these functions for cache management and retrieving current NFL timing information.

    FunctionDescription
    clear_cache()Clear cached data
    get_current_season()Get current NFL season
    get_current_week()Get current NFL week