nba-api
nba-api is a free and active Python client package designed to access statistics from the Official NBA stats page (nba.com). It provides a robust interface for developers to retrieve data on NBA teams, seasons, players, and games in various formats, including pandas DataFrames. The library is actively maintained with frequent updates to adapt to changes in the underlying NBA.com API.
Common errors
-
AttributeError: module 'nba_api.stats.endpoints' has no attribute 'BoxScorePlayerTrackV2'
cause You are attempting to use an NBA API endpoint that has been removed from the `nba-api` library due to discontinuation by NBA.com.fixUpdate your code to use the replacement endpoint, `BoxScorePlayerTrackV3`. For other removed endpoints like `PlayerFantasyProfile`, there may not be a direct replacement; refer to `nba-api` release notes for guidance. -
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='stats.nba.com', port=443): Read timed out. (read timeout=30)
cause Your requests are either being blocked by NBA.com due to an outdated `User-Agent` header, or you are hitting aggressive rate limits, especially common from cloud IP addresses.fixUpgrade `nba-api` to v1.11.4+ for updated headers. If the issue persists, introduce `time.sleep()` delays between requests (e.g., 1-3 seconds) and consider running from a local IP or using proxies if deployed on a cloud server. -
KeyError: 'Some_Expected_Column_Name' or unexpected data in DataFrame columns for BoxScore endpoints.
cause In `nba-api` v1.11.2, the order of datasets returned by certain `BoxScore` parsers (e.g., `BoxScoreAdvancedV3`, `BoxScoreDefensiveV2`, `BoxScoreFourFactorsV3`) was fixed. Your code might be accessing data from the wrong index in the list of DataFrames.fixEnsure you are using `nba-api` v1.11.2 or newer. After calling an endpoint, inspect the list of DataFrames returned by `get_data_frames()` (e.g., `endpoint.get_data_frames()`) to confirm the current order and adjust your indexing accordingly. For example, player stats might be `[0]` and team stats `[1]`.
Warnings
- breaking Several API endpoints, including `BoxScorePlayerTrackV2` and `PlayerFantasyProfile`, have been removed due to NBA.com API discontinuation. `PlayByPlayV2` and `ScoreboardV2` are also deprecated, requiring migration to their `V3` counterparts.
- gotcha NBA.com has updated its HTTP header requirements, causing requests from older `nba-api` versions to be blocked with timeout or forbidden errors (e.g., outdated User-Agent).
- gotcha A critical bug fix in v1.11.2 altered the dataset order returned by several parser functions (e.g., `BoxScoreAdvancedV3`, `BoxScoreDefensiveV2`, `BoxScoreFourFactorsV3`). If your code expects a specific order (e.g., `[0]` for player stats, `[1]` for team stats), it might now receive incorrect data.
- gotcha NBA.com's API can aggressively rate-limit or temporarily block IP addresses, especially from cloud environments, leading to `Read timed out` or `403 Forbidden` errors for multiple consecutive requests.
Install
-
pip install nba-api
Imports
- PlayerCareerStats
from nba_api.stats.endpoints import playercareerstats
- get_players
from nba_api.stats.static import players
- ScoreBoard
from nba_api.live.nba.endpoints import scoreboard
Quickstart
from nba_api.stats.static import players
from nba_api.stats.endpoints import playercareerstats
# Find LeBron James' player ID
nba_players = players.get_players()
lebron = [player for player in nba_players if player['full_name'] == 'LeBron James'][0]
lebron_id = lebron['id']
# Get LeBron James' career stats
career_stats = playercareerstats.PlayerCareerStats(player_id=lebron_id)
# Access data as a pandas DataFrame (requires pandas installed)
career_df = career_stats.get_data_frames()[0]
print(f"LeBron James' Career Stats (first 5 rows):\n{career_df.head()}")
# Access data as a JSON dictionary
career_json = career_stats.get_json()
# print(f"\nLeBron James' Career Stats (JSON excerpt):\n{career_json[:200]}...")