Exemple #1
0
    def test_get_standings(self):
        d = get_standings()
        self.assertListEqual(list(d.keys()), ['EASTERN_CONF', 'WESTERN_CONF'])

        df = d['WESTERN_CONF']
        expected_columns = ['TEAM', 'W', 'L', 'W/L%', 'GB', 'PW', 'PL', 'PS/G', 'PA/G']
        self.assertListEqual(list(df.columns), expected_columns)
 def get_team_standings(self, subset_by_seasons: list = None):
     """Assumptions : the season is over by June 1st.
     TODO : Use the season dataset to find last game date.
     """
     year = datetime.datetime.now().year
     allowed_seasons = range(1974, year + 1)
     if subset_by_seasons is not None:
         seasons = [
             season for season in subset_by_seasons
             if season in allowed_seasons
         ]
     else:
         seasons = allowed_seasons
     total_dfs = []
     for season in seasons:
         print("Retrieving standings of season", season, "...")
         date = "06-01-" + str(season)
         dfs = []
         results = get_standings(date=date)
         for conference, data in results.items():
             data = data.dropna(axis="index", how="any")
             data = data.sort_values(by="W/L%", ascending=False)
             data = data.reset_index(drop=True)
             data.loc[:, "CONF"] = conference
             data.loc[:, "CONF_RANK"] = data.index + 1
             data.loc[:, "TEAM"] = data["TEAM"].str.upper().str.replace(
                 "[^A-Z]", "")
             team_names = {}
             for raw, short in self.team_names.items():
                 raw = "".join(filter(str.isalpha, raw)).upper()
                 team_names[raw] = short
             data = data[~data["TEAM"].str.contains("DIVISION")]
             unmapped_teams = [
                 team for team in data["TEAM"].unique()
                 if team not in team_names.keys()
             ]
             data.loc[:, "TEAM"] = data["TEAM"].map(team_names)
             if data["TEAM"].isna().sum() > 0:
                 raise ValueError("Unknown/unmapped teams : %s",
                                  unmapped_teams)
             data.loc[:, "GB"] = (data["GB"].str.replace("—", "0.0").astype(
                 float, errors="raise"))
             data.loc[:, "TEAM_SEASON"] = data["TEAM"] + "_" + str(season)
             data.loc[:, "SEASON"] = season
             data = data.set_index("TEAM_SEASON", drop=True)
             dfs.append(data)
         all_conf_df = pandas.concat(dfs,
                                     join="outer",
                                     axis="index",
                                     ignore_index=False)
         total_dfs.append(all_conf_df)
     all_conf_df = pandas.concat(total_dfs,
                                 join="outer",
                                 axis="index",
                                 ignore_index=False)
     return all_conf_df
Exemple #3
0
    def test_get_standings(self):
        d = get_standings()
        self.assertListEqual(list(d.keys()),
                             ['Eastern Conference', 'Western Conference'])

        df = d['Western Conference']
        expected_columns = [
            'Western Conference', 'W', 'L', 'W/L%', 'GB', 'PW', 'PL', 'PS/G',
            'PA/G'
        ]
        self.assertListEqual(list(df.columns), expected_columns)
Exemple #4
0
def get_team_list():
    from basketball_reference_scraper.seasons import get_standings

    df = get_standings("2019-01-01")

    east = df['EASTERN_CONF']
    west = df['WESTERN_CONF']

    east2 = east.TEAM.str.rstrip("*").to_list()
    west2 = west.TEAM.str.rstrip("*").to_list()

    for i in range(0, len(west2)):
        east2.append(west2[i])

    return east2
Exemple #5
0
#Script to get NBA roster from a certain year
from basketball_reference_scraper.teams import get_roster
roster = get_roster('CLE', 2016)
print("\nCleveland Cavaliers 2016 Roster:")
print(roster)

#Script to retrieve an indiviual player's stats by year
from basketball_reference_scraper.players import get_stats
stats = get_stats('Collin Sexton', stat_type='PER_GAME')
print("\nCollin Sexton Stats:")
print(stats)

#Script to get NBA standing on a certain date
from basketball_reference_scraper.seasons import get_standings
standings = get_standings()
print("\nCurrent NBA Standings:")
print(standings)
Exemple #6
0
s = get_stats('Stephen Curry',
              stat_type='PER_GAME',
              playoffs=False,
              career=False)
print(s)

df = get_game_logs('LeBron James', '2010-01-19', '2014-01-20', playoffs=False)
print(df)

from basketball_reference_scraper.seasons import get_schedule, get_standings

s = get_schedule(2018, playoffs=False)
print(s)

s = get_standings(date='2020-01-06')
print(s)

from basketball_reference_scraper.box_scores import get_box_scores

s = get_box_scores('2020-01-13',
                   'CHI',
                   'BOS',
                   period='GAME',
                   stat_type='BASIC')
print(s)

from basketball_reference_scraper.pbp import get_pbp

s = get_pbp('2020-01-13', 'CHI', 'BOS')
print(s)
Exemple #7
0

start_year = 2010
end_year = 2013
df_columns = ['year', 'team', 'b2b_away_pct', 'b2b_away_wins', 'b2b_away_games', 'season_WL_pct', 'diff']
final_dict = {}
flat_final = pd.DataFrame(columns = df_columns)

#year = 2010

for year in range(start_year, end_year, 1):
    all_results = get_schedule(year, playoffs=False)
    teams = all_results.HOME.unique().tolist()
    
    date = str(year) + '-06-06'
    standings = get_standings(date=date)
    combined_standings = pd.concat([standings['EASTERN_CONF'], standings['WESTERN_CONF']])
    combined_standings.TEAM = combined_standings.TEAM.str.rstrip('*')

    final_dict[year] = {}    
    
    for team in teams:
    #team = 'Miami Heat'
    
        heat_home = all_results[all_results.HOME.eq(team)]
        heat_away = all_results[all_results.VISITOR.eq(team)]
        
        heat = all_results[(all_results['HOME']==team) | (all_results['VISITOR']==team)]
        
        num_games = heat.shape[0]