Home > database >  How to pull player game logs from multiple seasons using nba_api?
How to pull player game logs from multiple seasons using nba_api?

Time:12-05

I am trying to get familiar with nba_api package for python. I am attempting to pull player data from the past two seasons. However, I am only able to get all of the seasons or just one season.

First, I saw I could collect the game logs from an individual season:

from nba_api.stats.static import players

player_dict = players.get_players()

luka = [player for player in player_dict if player['full_name'] == 'Luka Doncic'][0]

luka_id = luka['id']

from nba_api.stats.endpoints import playergamelog

gamelog_luka = playergamelog.PlayerGameLog(player_id=luka_id, season='2022')

Doing this gave me his game logs from this current season. Next, I tried to collect the logs from the past two seasons:

gamelog_luka = playergamelog.PlayerGameLog(player_id=luka_id, season=['2020', '2022'])

I noticed that doing this only gave me 65 games starting from December 2020 to May 2021. I am trying to get the game logs from the beginning of the 2021 season to the present. Is there a syntax fix for this? Is there a way to achieve this through using season ID's?

CodePudding user response:

You can use SeasonAll, convert to pandas df, convert datetime and finally query.

import pandas as pd
from nba_api.stats.endpoints import playergamelog
from nba_api.stats.library.parameters import SeasonAll
from nba_api.stats.static import players


luka_id = next((x for x in players.get_players() if x.get("full_name") == "Luka Doncic"), None).get("id")

gamelog_luka = pd.concat(playergamelog.PlayerGameLog(player_id=luka_id, season=SeasonAll.all).get_data_frames())
gamelog_luka["GAME_DATE"] = pd.to_datetime(gamelog_luka["GAME_DATE"], format="%b %d, %Y")
gamelog_luka = gamelog_luka.query("GAME_DATE.dt.year in [2021, 2022]")
print(gamelog_luka)
  • Related