Exemple #1
0
def get_team_roster_by_year(team_id,
                            year,
                            raw=False,
                            overwrite=False,
                            verbose=False):
    '''
  Retrieves roster for a given team in a given season.
  
  :param team_id: NHL assigned id for the team
  :param year: season start year (e.g. 2018-2019 season would be 2018)
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of the roster for a given team in a given season
  '''
    base = 'https://statsapi.web.nhl.com/api/v1/'
    url = f'{base}teams/{team_id}/roster?season={year}{year+1}'
    response = make_request(url, overwrite=overwrite, verbose=verbose)
    if raw: return response['roster']
    df = pd.DataFrame.from_records(response['roster'])

    # Some df cleanup for easier analysis
    df['playerId'] = df['person'].apply(lambda x: x['id'])
    df['fullName'] = df['person'].apply(lambda x: x['fullName'])
    df['positionName'] = df['position'].apply(lambda x: x['name'])
    df['positionCode'] = df['position'].apply(lambda x: x['code'])
    df['positionType'] = df['position'].apply(lambda x: x['type'])
    df.drop(['person', 'position'], axis=1, inplace=True)
    df.insert(loc=0, column='teamId', value=team_id)
    return df
Exemple #2
0
def get_all_games_for_team_by_year(team_id,
                                   year,
                                   raw=False,
                                   overwrite=False,
                                   verbose=False):
    '''
  Retrieves game information for a given team in a given season. 
  
  :param team_id: NHL assigned id for the team
  :param year: season start year (e.g. 2018-2019 season would be 2018)
  :param raw: returns raw json response from web if True
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of game information for a given team in a given season.
  '''
    base = 'https://statsapi.web.nhl.com/api/v1/'
    url = f'{base}schedule?teamId={team_id}&season={year}{year+1}'
    response = make_request(url, overwrite=overwrite, verbose=verbose)

    if raw: return response['dates']
    df = pd.DataFrame.from_records(response['dates']).loc[:, ['date', 'games']]
    df['gameId'] = df['games'].apply(lambda x: x[0]['gamePk'])
    df['gameType'] = df['games'].apply(lambda x: x[0]['gameType'])
    df['season'] = df['games'].apply(lambda x: x[0]['season'])
    df['homeId'] = df['games'].apply(
        lambda x: x[0]['teams']['home']['team']['id'])
    df['awayId'] = df['games'].apply(
        lambda x: x[0]['teams']['away']['team']['id'])
    df['homeScore'] = df['games'].apply(
        lambda x: x[0]['teams']['home']['score'])
    df['awayScore'] = df['games'].apply(
        lambda x: x[0]['teams']['away']['score'])
    df['homeRecordWin'] = df['games'].apply(
        lambda x: x[0]['teams']['home']['leagueRecord']['wins'])
    df['homeRecordLoss'] = df['games'].apply(
        lambda x: x[0]['teams']['home']['leagueRecord']['losses'])
    df['homeRecordOT'] = df['games'].apply(lambda x : x[0]['teams']['home']['leagueRecord']['ot'] \
      if x[0]['gameType'] == 'R' and year >= 2005 else 0)
    df['awayRecordWin'] = df['games'].apply(
        lambda x: x[0]['teams']['away']['leagueRecord']['wins'])
    df['awayRecordLoss'] = df['games'].apply(
        lambda x: x[0]['teams']['away']['leagueRecord']['losses'])
    df['awayRecordOT'] = df['games'].apply(lambda x : x[0]['teams']['away']['leagueRecord']['ot'] \
      if x[0]['gameType'] == 'R' and year >= 2005 else 0)

    df['homeRecordTie'] = df['games'].apply(lambda x : x[0]['teams']['home']['leagueRecord']['ties'] \
      if x[0]['gameType'] == 'R' and year < 2005 else 0)
    df['awayRecordTie'] = df['games'].apply(lambda x : x[0]['teams']['away']['leagueRecord']['ties'] \
      if x[0]['gameType'] == 'R' and year < 2005 else 0)

    df['link'] = df['games'].apply(lambda x: x[0]['link'])
    df.drop(['games'], axis=1, inplace=True)
    df.insert(loc=0, column='teamId', value=team_id)
    return df
Exemple #3
0
def get_teams_by_year(year, raw=False, overwrite=False, verbose=False):
    '''
  Retrieves all teams that played in a given season.
  
  :param year: season start year (e.g. 2018-2019 season would be 2018)
  :param raw: returns raw json response from web if True
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of all teams that played in a given season
  '''
    base = 'https://statsapi.web.nhl.com/api/v1/'
    url = f'{base}teams?season={year}{year+1}'
    response = make_request(url, overwrite=overwrite, verbose=verbose)
    if raw: return response['teams']
    df = pd.DataFrame.from_records(response['teams'])
    df = df.rename(columns={'id': 'teamId'})
    return df
Exemple #4
0
def get_team_information(team_id, raw=False, overwrite=False, verbose=False):
    '''
  Retrieves information about a given team.
  
  :param team_id: NHL assigned id for the team
  :param raw: returns raw json response from web if True
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of information about a given team
  '''
    base = 'https://statsapi.web.nhl.com/api/v1/'
    url = f'{base}teams/{team_id}'
    response = make_request(url, overwrite=overwrite, verbose=verbose)
    if raw: return response['teams']
    df = pd.DataFrame.from_records(response['teams'])
    df = df.rename(columns={'id': 'teamId'})
    return df
Exemple #5
0
def get_player_game_level_statistics_by_year(player_id, year, raw=False, overwrite=False, verbose=False):
  '''
  Retrieves game level statistics for a given player for a given season.
  
  :param player_id: NHL assigned id for the player
  :param year: season start year (e.g. 2018-2019 season would be 2018)
  :param raw: returns raw json response from web if True
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of game level statistics about a given player
  '''
  base = 'https://statsapi.web.nhl.com/api/v1/'
  url = f'{base}people/{player_id}/stats?stats=gameLog&season={year}{year+1}'
  response = make_request(url, overwrite=overwrite, verbose=verbose)

  if 'stats' not in response.keys() or len(response['stats'][0]['splits']) == 0:
    raise PlayerNotFoundException(f'No game level statistics available for player \
      {player_id} for year {year}')

  if raw: return response['stats'][0]['splits']
  df = pd.DataFrame.from_records(response['stats'][0]['splits'])

  # Only extract useful fields
  df['gameId'] = df['game'].apply(lambda x : x['gamePk'])
  df['teamId'] = df['team'].apply(lambda x : x['id'])
  df['opponentTeamId'] = df['opponent'].apply(lambda x : x['id'])

  # Get consistent stat types
  stat_types = list(get_player_statistics_by_year(player_id, year, \
    overwrite=overwrite, verbose=verbose).keys())
  stat_types = [stat for stat in stat_types if 'PerGame' not in stat \
    and stat != 'playerId' and stat != 'season']
  for stat_type in stat_types:
    df[stat_type] = df['stat'].apply(lambda x : x[stat_type] if \
      stat_type in x.keys() else np.nan)

  df.insert(loc=0, column='playerId', value=player_id)
  df.drop(['game', 'team', 'opponent', 'stat'], axis=1, inplace=True)
  return df
Exemple #6
0
def get_team_statistics_rankings_by_year(team_id,
                                         year,
                                         raw=False,
                                         overwrite=False,
                                         verbose=False):
    '''
  Retrieves statistics rankings for a given team in a given season.
  
  :param team_id: NHL assigned id for the team
  :param year: season start year (e.g. 2018-2019 season would be 2018)
  :param raw: returns raw json response from web if True
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of statistics rankings for a given team in a given season
  '''
    base = 'https://statsapi.web.nhl.com/api/v1/'
    url = f'{base}teams/{team_id}/stats?season={year}{year+1}'
    response = make_request(url, overwrite=overwrite, verbose=verbose)
    if raw: return response['stats'][0]['splits'][0]['stat']
    df = pd.DataFrame.from_records(response['stats'][1]['splits'][0]['stat'],
                                   index=[0])
    df.insert(loc=0, column='teamId', value=team_id)
    return df
Exemple #7
0
def get_player_statistics_by_year(player_id, year, raw=False, overwrite=False, verbose=False):
  '''
  Retrieves statistics for a given player for a given season.
  
  :param player_id: NHL assigned id for the player
  :param year: season start year (e.g. 2018-2019 season would be 2018)
  :param raw: returns raw json response from web if True
  :param overwrite: overwrites cache if True
  :param verbose: shows logs if True
  :returns: dataframe of statistics about a given player
  '''
  base = 'https://statsapi.web.nhl.com/api/v1/'
  url = f'{base}people/{player_id}/stats?stats=statsSingleSeason&season={year}{year+1}'
  response = make_request(url, overwrite=overwrite, verbose=verbose)
  
  if 'stats' not in response.keys() or len(response['stats'][0]['splits']) == 0:
    raise PlayerNotFoundException(f'No statistics available for player \
      {player_id} for year {year}')

  if raw: return response['stats'][0]['splits'][0]['stat']
  df = pd.DataFrame.from_records(response['stats'][0]['splits'][0]['stat'], index=[0])
  df.insert(loc=0, column='playerId', value=player_id)
  df.insert(loc=0, column='season', value=f'{year}{year+1}')
  return df