def season_totals_to_csv(season):
    next_season = season + 1
    client.players_season_totals(
        season_end_year=season,
        output_type=OutputType.CSV,
        output_file_path="./" + season + "_" + next_season + "_player_season_totals.csv"
    )
Esempio n. 2
0
def download_yearly_stats(year, return_download_tables=False):
    downloaded_tables = {}

    output_file_path = output_base + f'/stats_by_year/{year}_totals.csv'
    if not os.path.isfile(output_file_path):
        downloaded_tables['totals'] = client.players_season_totals(
            year, output_type='csv', output_file_path=output_file_path)
        wait_random_time()

    output_file_path = output_base + f'/stats_by_year/{year}_advanced.csv'
    if not os.path.isfile(output_file_path):
        downloaded_tables['advanced'] = client.players_advanced_stats(
            year, output_type='csv', output_file_path=output_file_path)
        wait_random_time()

    ### repeat for the playoffs:
    output_file_path = output_base + f'/stats_by_year/{year}_playoffs_totals.csv'
    if not os.path.isfile(output_file_path):
        downloaded_tables['totals.playoffs'] = client.players_season_totals(
            year,
            playoffs=True,
            output_type='csv',
            output_file_path=output_file_path)
        wait_random_time()

    output_file_path = output_base + f'/stats_by_year/{year}_playoffs_advanced.csv'
    if not os.path.isfile(output_file_path):
        downloaded_tables['advanced.playoffs'] = client.players_advanced_stats(
            year,
            playoffs=True,
            output_type='csv',
            output_file_path=output_file_path)
        wait_random_time()

    ## now do the per 100 if they're aviailable
    if year >= 1974:
        output_file_path = output_base + f'/stats_by_year/{year}_per100.csv'
        if not os.path.isfile(output_file_path):
            downloaded_tables['per100'] = client.players_season_totals_per100(
                year, output_type='csv', output_file_path=output_file_path)
            wait_random_time()

        output_file_path = output_base + f'/stats_by_year/{year}_playoffs_per100.csv'
        if not os.path.isfile(output_file_path):
            downloaded_tables[
                'per100.playoffs'] = client.players_season_totals_per100(
                    year,
                    playoffs=True,
                    output_type='csv',
                    output_file_path=output_file_path)
            wait_random_time()

    if return_download_tables:
        return downloaded_tables
Esempio n. 3
0
    def assert_json(self):
        client.players_season_totals(
            season_end_year=self.year,
            output_type=OutputType.JSON,
            output_file_path=self.output_file_path,
        )

        with open(self.output_file_path, "r", encoding="utf8") as output_file, \
                open(self.expected_output_file_path, "r", encoding="utf8") as expected_output_file:
            self.assertEqual(
                json.load(output_file),
                json.load(expected_output_file),
            )
 def assert_player_season_totals_file(self):
     players_season_totals(
         season_end_year=self.year,
         output_type=OutputType.JSON,
         output_file_path=self.output_file_path,
         output_write_option=OutputWriteOption.WRITE,
     )
     with open(self.output_file_path, "r", encoding="utf8") as output_file, \
             open(self.expected_output_file_path, "r", encoding="utf8") as expected_output_file:
         self.assertEqual(
             json.load(output_file),
             json.load(expected_output_file),
         )
Esempio n. 5
0
 def assert_player_season_totals_file(self):
     players_season_totals(
         season_end_year=self.year,
         output_type=OutputType.CSV,
         output_file_path=self.output_file_path,
         output_write_option=OutputWriteOption.WRITE,
     )
     with open(self.output_file_path, "r") as output_file, \
             open(self.expected_output_file_path, "r") as expected_output_file:
         self.assertEqual(
             output_file.readlines(),
             expected_output_file.readlines(),
         )
Esempio n. 6
0
    def assert_csv(self):
        client.players_season_totals(
            season_end_year=self.year,
            output_type=OutputType.CSV,
            output_file_path=self.output_file_path,
        )

        with open(self.output_file_path, "r", encoding="utf8") as output_file, \
                open(self.expected_output_file_path, "r", encoding="utf8") as expected_output_file:
            self.assertEqual(
                output_file.readlines(),
                expected_output_file.readlines(),
            )
Esempio n. 7
0
def get_df_basic_player_stats(year):

    """
    Aggregates all relevant BASIC stats for players in given year
    :param year: Integer Ex.) 2021
    :return: DataFrame
    """

    basic = client.players_season_totals(season_end_year=year)

    df_basic = pd.json_normalize(basic)
    df_basic["position"] = df_basic["positions"].astype(str).str.split(":").str[1]
    df_basic["position"] = df_basic["position"].map(lambda x: x.lstrip(" '").rstrip(">]'"))

    cols_groupby = ["slug", "name", "age", "position"]
    cols_basic = ["games_played", "games_started", "minutes_played", "made_field_goals", "attempted_field_goals",
                "made_three_point_field_goals", "attempted_three_point_field_goals", "made_free_throws",
                "attempted_free_throws", "offensive_rebounds", "defensive_rebounds", "assists", "steals", "blocks",
                "turnovers", "personal_fouls", "points"]

    df_stats = df_basic.groupby(cols_groupby)[cols_basic].sum().reset_index()
    df_stats["points_per_game"] = df_stats["points"] / df_stats["games_played"]
    df_stats["assists_per_game"] = df_stats["assists"] / df_stats["games_played"]
    df_stats["rebounds"] = df_stats["offensive_rebounds"] + df_stats["defensive_rebounds"]
    df_stats["rebounds_per_game"] = df_stats["rebounds"] / df_stats["games_played"]
    df_stats["made_three_point_field_goals_per_game"] = df_stats["made_three_point_field_goals"] / df_stats["games_played"]

    df_stats["field_goal_pct"] = df_stats["made_field_goals"] / df_stats["attempted_field_goals"]
    df_stats["field_goal_pct_three_pt"] = df_stats["made_three_point_field_goals"] / df_stats["attempted_three_point_field_goals"]

    df_stats.to_pickle(c.PICKLE_PATH_BASIC_STATS)

    return df_stats
Esempio n. 8
0
def output(request):
    userskill = "passing"
    userposition = "POINT GUARD"
    skillsdict = {
        "scoring": "points",
        "passing": "assists",
        "fouling": "fouls",
    }
    skilltrans = skillsdict[userskill]
    raw = client.players_season_totals(season_end_year=2020,
                                       output_type=OutputType.JSON)
    data = json.loads(raw)
    data.sort(key=operator.itemgetter(skilltrans), reverse=True)
    top25 = data[:25]
    output = json.dumps(top25, indent=2)
    positions = json.loads(output)
    result = []

    for player in positions:
        if userposition in player['positions']:
            result.append(player['name'])
            continue
        else:
            continue

    print(random.choice(result))
    data = (random.choice(result))
    return render(request, 'nbacomp/home.html', {'data': data})
def get_rookie_stats(year, rookie_names):
    season_totals = client.players_season_totals(season_end_year=year)
    stats_dict = {}
    for total in season_totals:
        if total['name'] in rookie_names and total['games_played'] >= 30:
            stats_dict[total['name']] = total
    return stats_dict
Esempio n. 10
0
def player_stats():
    print("collecting player season totals")
    client.players_season_totals(
        season_end_year=2020,
        output_type=OutputType.CSV,
        output_file_path=
        "C:\\Users\\NWHAL\\Documents\\nba_project\\2020_player_season_totals.csv"
    )

    print("collecting advanced player stats")
    client.players_advanced_season_totals(
        season_end_year=2020,
        output_type=OutputType.CSV,
        output_file_path=
        "C:\\Users\\NWHAL\\Documents\\nba_project\\2020_advanced_player_season_totals.csv"
    )
Esempio n. 11
0
 def test_last_2001_player_season_totals(self):
     player_season_totals = client.players_season_totals(season_end_year=2001)
     self.assertEqual(
         player_season_totals[len(player_season_totals) - 1],
         {
             "slug": "zhizhwa01",
             "name": "Wang Zhizhi",
             "positions": [Position.CENTER],
             "age": 23,
             "team": Team.DALLAS_MAVERICKS,
             "games_played": 5,
             "games_started": 0,
             "minutes_played": 38,
             "made_field_goals": 8,
             "attempted_field_goals": 19,
             "made_three_point_field_goals": 0,
             "attempted_three_point_field_goals": 2,
             "made_free_throws": 8,
             "attempted_free_throws": 10,
             "offensive_rebounds": 1,
             "defensive_rebounds": 6,
             "assists": 0,
             "steals": 0,
             "blocks": 0,
             "turnovers": 1,
             "personal_fouls": 8,
             "points": 24,
         }
     )
Esempio n. 12
0
    def test_avery_bradley_2019_player_season_totals(self):
        player_season_totals = client.players_season_totals(season_end_year=2019)
        clippers_avery_bradley = player_season_totals[66]

        self.assertEqual('bradlav01', clippers_avery_bradley["slug"])
        self.assertEqual("Avery Bradley", clippers_avery_bradley["name"])
        self.assertListEqual([Position.SHOOTING_GUARD], clippers_avery_bradley["positions"])
        self.assertEqual(28, clippers_avery_bradley["age"])
        self.assertEqual(Team.LOS_ANGELES_CLIPPERS, clippers_avery_bradley["team"])
        self.assertEqual(49, clippers_avery_bradley["games_played"])
        self.assertEqual(49, clippers_avery_bradley["games_started"])
        self.assertEqual(1463, clippers_avery_bradley["minutes_played"])
        self.assertEqual(161, clippers_avery_bradley["made_field_goals"])
        self.assertEqual(420, clippers_avery_bradley["attempted_field_goals"])
        self.assertEqual(58, clippers_avery_bradley["made_three_point_field_goals"])
        self.assertEqual(172, clippers_avery_bradley["attempted_three_point_field_goals"])
        self.assertEqual(20, clippers_avery_bradley["made_free_throws"])
        self.assertEqual(25, clippers_avery_bradley["attempted_free_throws"])
        self.assertEqual(35, clippers_avery_bradley["offensive_rebounds"])
        self.assertEqual(96, clippers_avery_bradley["defensive_rebounds"])
        self.assertEqual(96, clippers_avery_bradley["assists"])
        self.assertEqual(27, clippers_avery_bradley["steals"])
        self.assertEqual(16, clippers_avery_bradley["blocks"])
        self.assertEqual(61, clippers_avery_bradley["turnovers"])
        self.assertEqual(133, clippers_avery_bradley["personal_fouls"])
        self.assertEqual(400, clippers_avery_bradley["points"])
Esempio n. 13
0
 def test_players_season_totals_json(self):
     result = players_season_totals(season_end_year=2018, output_type=OutputType.JSON)
     with open(self.expected_output_file_path, "r") as expected_results_file:
         self.assertEqual(
             json.loads(result),
             json.load(expected_results_file)
         )
Esempio n. 14
0
def regularSeasonPlayer(playerName):
    season19 = client.players_season_totals(season_end_year=2019)
    playerStats = []
    points = 0
    rebounds = 0

# for x in range(len(player_list)):
#    if(player_list[x]['team'] == Team.TORONTO_RAPTORS ):
#        print (player_list[x]['name'])

    # assists
    for x in range(len(season19)):
        if(season19[x]['name'].replace(" ", "").upper() == playerName):
            playerStats.append(
                round(((season19[x]['assists'])/(season19[x]['games_played'])), 1))
            # print((season19[x]['assists']))

    # points
    for x in range(len(season19)):
        if(season19[x]['name'].replace(" ", "").upper() == playerName):
            points = (points + (season19[x]['made_field_goals']-season19[x]['made_three_point_field_goals'])*2 + (
                season19[x]['made_three_point_field_goals'])*3 + (season19[x]['made_free_throws'])*1)/(season19[x]['games_played'])
            playerStats.append(round(points, 1))
            # print (points)

    # rebounds
    for x in range(len(season19)):
        if(season19[x]['name'].replace(" ", "").upper() == playerName):
            rebounds = (((season19[x]['offensive_rebounds'])) + (
                (season19[x]['defensive_rebounds'])))/(season19[x]['games_played'])
            playerStats.append(round(rebounds, 1))
            # print (rebounds)

    return playerStats
Esempio n. 15
0
 def test_first_2001_player_season_totals(self):
     player_season_totals = client.players_season_totals(season_end_year=2001)
     self.assertEqual(
         player_season_totals[0],
         {
             "slug": "abdulma02",
             "name": "Mahmoud Abdul-Rauf",
             "positions": [Position.POINT_GUARD],
             "age": 31,
             "team": Team.VANCOUVER_GRIZZLIES,
             "games_played": 41,
             "games_started": 0,
             "minutes_played": 486,
             "made_field_goals": 120,
             "attempted_field_goals": 246,
             "made_three_point_field_goals": 4,
             "attempted_three_point_field_goals": 14,
             "made_free_throws": 22,
             "attempted_free_throws": 29,
             "offensive_rebounds": 5,
             "defensive_rebounds": 20,
             "assists": 76,
             "steals": 9,
             "blocks": 1,
             "turnovers": 26,
             "personal_fouls": 50,
             "points": 266,
         }
     )
Esempio n. 16
0
def avg_20_plus():
    for x in (client.players_season_totals(season_end_year=2020)):
        games_played = int(x['games_played'])
        points = int(x['points'])
        avg_points = round(points / games_played, 2)

        if (avg_points > 20):
            print(x['name'], "avg_points is", str(avg_points))
    def test_2018_player_season_totals(self):
        now = datetime.now()
        current_year = now.year

        for year in range(2001, current_year + 1):
            player_season_totals = client.players_season_totals(
                season_end_year=year)
            self.assertIsNotNone(player_season_totals)
Esempio n. 18
0
def avg_20_plus_mins():
    for x in (client.players_season_totals(season_end_year=2020)):
        minutes_played = x["minutes_played"]
        games_played = x['games_played']
        avg_minutes_played = round(minutes_played / games_played, 2)

        if (avg_minutes_played > 20):
            print(x['name'], avg_minutes_played)
def get_roster_player_list(team):
    client.players_advanced_season_totals(
        season_end_year=2020,
        output_type=OutputType.CSV,
        output_file_path="./csv/advanced_players.csv")

    client.players_season_totals(season_end_year=2020,
                                 output_type=OutputType.CSV,
                                 output_file_path="./csv/season_totals.csv")

    df_advanced_stats = pd.read_csv("./csv/advanced_players.csv")
    df_total_season_stats = pd.read_csv("./csv/season_totals.csv")

    players_advanced_roster_stats = df_advanced_stats.loc[
        df_advanced_stats['team'].str.contains(team.name[1].upper())]
    players_total_roster_stats = df_total_season_stats.loc[
        df_advanced_stats['team'].str.contains(team.name[1].upper())]

    return players_advanced_roster_stats, players_total_roster_stats
Esempio n. 20
0
def steals_vs_fouls():
    df = DataFrame()
    for x in (client.players_season_totals(season_end_year=2020)):
        df = df.append(DataFrame(x), ignore_index=True)

    df['avg_steals'] = df['steals'] / df['games_played']
    df['avg_fouls'] = df['personal_fouls'] / df['games_played']

    df.plot(x='avg_steals', y='avg_fouls', kind='scatter')
    plt.show()
    def assert_json(self):
        results = client.players_season_totals(
            season_end_year=self.year,
            output_type=OutputType.JSON,
        )

        with open(self.expected_output_file_path, "r") as expected_output_file:
            self.assertEqual(
                json.loads(results),
                json.load(expected_output_file),
            )
Esempio n. 22
0
def get_values(player_list_string):
    values = []

    player_list = player_list_string.split(",")
    player_list = [x.lower().strip() for x in player_list
                   ]  #iterates over list and makes everything lower()

    players = client.players_season_totals(YEAR_END)
    for player in players:
        if player['name'].lower() in player_list:
            values.append(player)
    return values
Esempio n. 23
0
def plot_usage_vs_points():
    df = DataFrame()
    df2 = DataFrame()
    for x in (client.players_advanced_season_totals(season_end_year=2020)):
        df = df.append(DataFrame(x), ignore_index=True)

    for x in (client.players_season_totals(season_end_year=2020)):
        df2 = df2.append(DataFrame(x), ignore_index=True)

    df['avg_points'] = round(df2['points'] / df2['games_played'], 2)
    df.plot(x='usage_percentage', y='avg_points', kind='scatter')
    plt.show()
Esempio n. 24
0
 def create_player_totals_csv(year_start, year_end, advanced=False):
     for i in range(year_start, year_end + 1):
         if advanced:
             if not os.path.exists("data/adv_total_stats"):
                 os.makedirs("data/adv_total_stats")
             client.players_advanced_season_totals(
                 season_end_year=i,
                 output_type=OutputType.CSV,
                 output_file_path="adv_total_stats/stats_{}.csv".format(i))
         else:
             if not os.path.exists("data/total_stats"):
                 os.makedirs("data/total_stats")
             client.players_season_totals(
                 season_end_year=i,
                 output_type=OutputType.CSV,
                 output_file_path="total_stats/stats_{}.csv".format(i))
         # self.feed_season_stats_to_db(stats, i)
         print(
             "Exported stats for the year {}. {} percent completed".format(
                 i, 100 * ((i - year_start + 1) /
                           (year_end - year_start + 1))))
Esempio n. 25
0
def points_per_minutes():
    answer_dict = {}
    for x in (client.players_season_totals(season_end_year=2020)):
        minutes_played = x['minutes_played']
        total_points = x['points']
        avg_points_per_min = round(total_points / minutes_played, 2)

        answer_dict[x['name']] = avg_points_per_min
    sorted_answer = sorted(answer_dict.items(), key=operator.itemgetter(1))

    pprint("is points per minute")
    pprint(sorted_answer)
    pprint("is points per minute")
Esempio n. 26
0
def under_20_avg_20_plus():
    #we put the answer in a dictionary, because we want to sort it
    answer_dict = {}
    for x in (client.players_season_totals(season_end_year=2020)):
        age = x['age']
        points = x['points']
        games_played = x['games_played']
        avg_points = round(points / games_played, 2)

        if (age <= 20 and avg_points >= 20):
            answer_dict[x['name']] = avg_points

    pprint(answer_dict)
def clean_basic(year):
    basic_stats = client.players_season_totals(season_end_year=year)
    df = pd.DataFrame(basic_stats)
    df = df.groupby('name').agg('sum')
    num_cols = list(df.select_dtypes(include=['int', 'float64']))
    unwanted_num_cols = [
        'age', 'minutes_played', 'games_played', 'games_started'
    ]
    for col in unwanted_num_cols:
        num_cols.remove(col)
    for col in num_cols:
        df[col] = (df[col] / df['minutes_played']) * 48
    df['year'] = year
    return df
def find_player_season_stats(year_start, year_end):

    years = [i for i in range(year_start, year_end + 1)]
    player_stats = pd.DataFrame()

    for i in years:
        temp = pd.DataFrame(client.players_season_totals(season_end_year=i))
        temp['season_end_year'] = i

        player_stats = pd.concat([player_stats, temp], axis=0)

    player_stats = player_stats.reset_index()
    player_stats = player_stats.drop(columns=['index'])

    return player_stats
Esempio n. 29
0
def get_season_totals():
    """
    Scrapes season total stats for all seasons since 1950
    """
    df = pd.DataFrame()
    for year in range(1950, 2020):
        players = client.players_season_totals(season_end_year=year)
        for player in players:
            player["year"] = year
            try:
                player["team"] = player["team"].value
            except:
                None
            df = df.append(player, ignore_index=True)
    return df
Esempio n. 30
0
def plot_3_pct_vs_points():
    df = DataFrame()
    df_3s = DataFrame()

    for x in (client.players_season_totals(season_end_year=2019)):
        df = df.append(DataFrame(x), ignore_index=True)

    df['3_pct'] = df['made_three_point_field_goals'] / df[
        'attempted_three_point_field_goals']
    df['avg_points'] = df['points'] / df['games_played']
    #df_temp = DataFrame(1,2)
    #df = df.append(df_temp)
    #df.plot(x='turnovers',y='points')
    df.plot(x='3_pct', y='avg_points', kind='scatter')
    plt.show()