MLB-StatsAPI icon indicating copy to clipboard operation
MLB-StatsAPI copied to clipboard

lookup_player() throws exception "list index out of range" prior to season starting

Open seanloosier opened this issue 6 years ago • 7 comments

I'm having some trouble with lookup_player() throwing an exception - I'm using the example code from statsapi/init.py

player = statsapi.lookup_player('nola,')
print(player[0]['id']) #assume only 1 record returned for demo purposes

some other calls are working - for example

print(statsapi.meta('leagueLeaderTypes'))

returns a list of League Leader Types

so, I'm thinking the problem is somewhere in the code maybe in get() or maybe the underlying MLB stats API has changed somehow. It's taking me a while to try to figure this out on my own so any help would be appreciated.

seanloosier avatar Mar 07 '20 21:03 seanloosier

This is most likely because gameType defaults to R (regular season) and season defaults to current calendar year. Include season=2018 to look up players from last season, and/or gameType=S (I think S is Spring Training but I may be remembering incorrectly).

player = statsapi.lookup_player('nola,', season=2018)

toddrob99 avatar Mar 07 '20 23:03 toddrob99

changing the season to 2018 or the gameType to works. That saved me a lot of fiddling around, thank you!

seanloosier avatar Mar 07 '20 23:03 seanloosier

I’ll try to think of a good way to refactor this function so it works prior to the season starting. Unfortunately there is no endpoint to search players from all time, so it has to specify a season at least. In order to make the function more robust I could probably check the season state and default to previous season if it hasn’t started yet for the current calendar year.

toddrob99 avatar Mar 07 '20 23:03 toddrob99

wasnt sure if i should add a new issue as mine is similar. Mine happens on last_game, but since it only takes one parameter, teamId, i am unable to add gametype.

McFlyin619 avatar Mar 01 '21 07:03 McFlyin619

wasnt sure if i should add a new issue as mine is similar. Mine happens on last_game, but since it only takes one parameter, teamId, i am unable to add gametype.

@McFlyin619, this is actually functioning as designed. The last_game() method is pulling the team endpoint, and returning data from the previousSchedule node--not calculating the last game on its own. If that node is missing from the result, or contains no game data, an exception will be raised. The MLB-StatsAPI library does not attempt to catch all exceptions due to missing data in the API results; instead that should be handled by the calling application when needed.

toddrob99 avatar Mar 01 '21 16:03 toddrob99

Ahhh, ok. I'll add the previousSchedule to the results. Thank you!

McFlyin619 avatar Mar 01 '21 20:03 McFlyin619

In v1.5 (publishing in the next day or two) will change the default for lookup_player and lookup_team methods to the latest season based on the season endpoint, rather than the current calendar year. Will not be able to fully test until January, so please submit a new issue if needed.

toddrob99 avatar Aug 10 '22 17:08 toddrob99