Beispiel #1
0
def load_customized_data(teamName, startYear, endYear):
    home_team = []
    away_team = []
    home_team_home_record_pct = []
    away_team_away_record_pct = []
    home_team_current_win_percentage = []
    away_team_current_win_percentage = []
    home_team_current_standing = []
    away_team_current_standing = []
    home_team_win_percentage_streak_over_last_n_games = []
    away_team_win_percentage_streak_over_last_n_games = []
    home_team_current_streak = []
    away_team_current_streak = []
    recent_head_to_head_wrt_home_team = []

    df = get_teamBoxScore(teamName, get_season(startYear))
    time.sleep(0.5)
    for index, row in df.iterrows():
        game_id = row["Game_ID"]
        print("game_id", game_id)
        game_summary = game.BoxscoreSummary(game_id=game_id).game_summary()
        time.sleep(0.5)
        game_summary = game_summary.iloc[0]

        home_team_id = game_summary["HOME_TEAM_ID"]

        away_team_id = game_summary["VISITOR_TEAM_ID"]
        home_team.append(home_team_id)
        away_team.append(away_team_id)
        date = datetime.datetime.strptime(row['GAME_DATE'], "%b %d, %Y")
        year, month, day = date.year, date.month, date.day
        scoreboard = Scoreboard(month=month, day=day, year=year)
        time.sleep(0.5)
        if IdToConference[str(home_team_id)] == 'Eastern':
            day_home_stats = scoreboard.east_conf_standings_by_day()
        else:
            day_home_stats = scoreboard.west_conf_standings_by_day()

        if IdToConference[str(away_team_id)] == 'Eastern':
            day_away_stats = scoreboard.east_conf_standings_by_day()
        else:
            day_away_stats = scoreboard.west_conf_standings_by_day()

        home_index = np.flatnonzero(
            day_home_stats['TEAM_ID'] == home_team_id)[0]
        away_index = np.flatnonzero(
            day_away_stats['TEAM_ID'] == away_team_id)[0]
        day_home_team_stats = day_home_stats.iloc[home_index]
        day_away_team_stats = day_away_stats.iloc[away_index]
        #print("idx::",day_home_team_stats)
        home_team_current_win_percentage.append(day_home_team_stats["W_PCT"])
        away_team_current_win_percentage.append(day_away_team_stats["W_PCT"])
        home_team_current_standing.append(home_index + 1)
        away_team_current_standing.append(away_index + 1)
        #print ("hhghg:",day_home_team_stats["HOME_RECORD"])
        home_wins, home_losses = map(
            int, day_home_team_stats["HOME_RECORD"].split('-'))
        away_wins, away_losses = map(
            int, day_away_team_stats["ROAD_RECORD"].split('-'))
        home_team_home_w_pct = 0
        away_team_away_w_pct = 0
        if home_wins + home_losses:
            home_team_home_w_pct = home_wins / (home_wins + home_losses)
        if away_wins + away_losses:
            away_team_away_w_pct = away_wins / (away_wins + away_losses)

        home_team_home_record_pct.append(home_team_home_w_pct)
        away_team_away_record_pct.append(away_team_away_w_pct)

    for i in range(endYear - startYear):
        season = get_season(startYear + 1 + i)
        print("season:::", season)
        additional_data = get_teamBoxScore(teamName, season)
        time.sleep(0.5)
        for index, row in additional_data.iterrows():
            game_id = row["Game_ID"]
            print("game_id::", game_id)
            game_summary = game.BoxscoreSummary(game_id=game_id).game_summary()
            time.sleep(0.5)
            game_summary = game_summary.iloc[0]
            home_team_id = game_summary["HOME_TEAM_ID"]
            away_team_id = game_summary["VISITOR_TEAM_ID"]
            home_team.append(home_team_id)
            away_team.append(away_team_id)
            date = datetime.datetime.strptime(row['GAME_DATE'], "%b %d, %Y")
            year, month, day = date.year, date.month, date.day
            scoreboard = Scoreboard(month=month, day=day, year=year)
            time.sleep(0.5)
            day_stats = None
            if IdToConference[str(home_team_id)] == 'Eastern':
                day_home_stats = scoreboard.east_conf_standings_by_day()
            else:
                day_home_stats = scoreboard.west_conf_standings_by_day()

            if IdToConference[str(away_team_id)] == 'Eastern':
                day_away_stats = scoreboard.east_conf_standings_by_day()
            else:
                day_away_stats = scoreboard.west_conf_standings_by_day()

            try:
                home_index = np.flatnonzero(
                    day_home_stats['TEAM_ID'] == home_team_id)[0]
            except:
                print("home_team_id::", home_team_id)
                print("stats::", day_home_stats)
                print("game_id:::", game_id, game_summary)
                raise Exception("sha")
            away_index = np.flatnonzero(
                day_away_stats['TEAM_ID'] == away_team_id)[0]
            day_home_team_stats = day_home_stats.iloc[home_index]
            day_away_team_stats = day_home_stats.iloc[away_index]
            home_team_current_win_percentage.append(
                day_home_team_stats["W_PCT"])
            away_team_current_win_percentage.append(
                day_away_team_stats["W_PCT"])
            home_team_current_standing.append(home_index + 1)
            away_team_current_standing.append(away_index + 1)
            home_wins, home_losses = map(
                int, day_home_team_stats["HOME_RECORD"].split('-'))
            away_wins, away_losses = map(
                int, day_away_team_stats["ROAD_RECORD"].split('-'))
            home_team_home_w_pct = 0
            away_team_away_w_pct = 0
            if home_wins + home_losses:
                home_team_home_w_pct = home_wins / (home_wins + home_losses)
            if away_wins + away_losses:
                away_team_away_w_pct = away_wins / (away_wins + away_losses)

            home_team_home_record_pct.append(home_team_home_w_pct)
            away_team_away_record_pct.append(away_team_away_w_pct)

        df = df.append(additional_data, ignore_index=True)

    home_team_series = pd.Series(home_team)
    away_team_series = pd.Series(away_team)
    home_team_home_record_pct_series = pd.Series(home_team_home_record_pct)
    away_team_away_record_pct_series = pd.Series(away_team_away_record_pct)
    home_team_current_win_percentage_series = pd.Series(
        home_team_current_win_percentage)
    away_team_current_win_percentage_series = pd.Series(
        away_team_current_win_percentage)
    home_team_current_standing_series = pd.Series(home_team_current_standing)
    away_team_current_standing_series = pd.Series(away_team_current_standing)

    print("length:::", len(home_team_series.values))
    print("df_length:::", df.index)
    df = df.assign(home_team=home_team_series.values)
    df = df.assign(away_team=away_team_series.values)
    df = df.assign(
        home_team_home_record_pct=home_team_home_record_pct_series.values)
    df = df.assign(
        away_team_home_record_pct=away_team_away_record_pct_series.values)
    df = df.assign(
        home_team_current_win_percentage=home_team_current_win_percentage_series
        .values)
    df = df.assign(
        away_team_current_win_percentage=away_team_current_win_percentage_series
        .values)
    df = df.assign(
        home_team_current_standing_series=home_team_current_standing_series.
        values)
    df = df.assign(
        away_team_current_standing_series=away_team_current_standing_series.
        values)

    print("headers:::", list(df))
    return df
Beispiel #2
0
def main():


    print('Welcome to Abhi\'s NBA CLI!')
    loop=True
    while loop:
        # print('What would you like to do?')
        # print('1. Get information about a player')
        # # print()
        # print('2. View Completed/In Progress Games')
        # print('3. View Today\'s Upcoming Games')
        # # print()
        # print('9. Exit the program')
        # print('100. Used for testing')
        print_main_menu()
        main_choice = input("Pick a number from the list above\n")
        if int(main_choice) == 1:
            first_name = input("What is the first name of the player you'd like to view?\n")
            last_name = input("What is the last name?\n")

            print_player_information(first_name.strip(), last_name.strip())

        elif int(main_choice) == 2:
            print_scores()

        elif int(main_choice) == 3:
            print_upcoming_games()

        elif int(main_choice) == 4:
            team_choice = input("Enter the name of the team you'd like to view (ex. Boston Celtics)\n")
            print_team_information(team_choice)

        elif int(main_choice) == 5:
            board = Scoreboard()
            printer.pprint(board.east_conf_standings_by_day())
            
        elif int(main_choice) == 6:
            board = Scoreboard()
            printer.pprint(board.west_conf_standings_by_day())

        elif int(main_choice) == 9:
            print("Thank you for using Abhi's NBA Stats CLI!")
            return

        elif int(main_choice) == 100:
            # team_game_logs = team.TeamGameLogs("1610612738")
            # # print(type(team_game_logs))
            # printer.pprint(team_game_logs.info())
            # teamcommonroster = team.TeamCommonRoster("1610612738", season='2017-18')
            # coaches = teamcommonroster.coaches()
            # roster = teamcommonroster.roster()
            # print(coaches)
            # printer.pprint(roster)
            teamlist = team.TeamList(league_id='00')
            printer.pprint(teamlist.info())

        else:
            print("Invalid menu choice")


    last_choice = input("Would you like to run the program again? Press 1 to run again, or 2 to exit\n")
    if int(last_choice) == 1:
        main()
    elif int(last_choice) == 2:
        print("Thank you for using Abhi's NBA Stats CLI!")
        return