fivethirtyeight / nfl-elo-game

Data and code for FiveThirtyEight's NFL game
MIT License
319 stars 365 forks source link

key ' ' error #4

Closed nixonth closed 6 years ago

nixonth commented 6 years ago

Hello,

Hoping someone could help me out with an error. When I delete the three #'s in util.py, I get a key error

Traceback (most recent call last):
  File "eval.py", line 8, in <module>
    Forecast.forecast(games)
  File "...data/forecast.py", line 27, in forecast
    team1, team2 = teams[game['team1']] , teams[game['team2']]
KeyError: ''

Line 27 is the last provided here:

        for game in games:
            team1, team2 = teams[game['team1']] , teams[game['team2']]

I thought it may have something to do with how teams is being defined, so rows 11 - 27 are included below.

class Forecast:

    @staticmethod
    def forecast(games):
        """ Generates win probabilities in the my_prob1 field for each game based on Elo model """

        # Initialize team objects to maintain ratings
        teams = {}
        for row in [item for item in csv.DictReader(open("data/initial_elos.csv"))]:
            teams[row['team']] = {
                'name': row['team'],
                'season': None,
                'elo': float(row['elo'])
            }

        for game in games:
            team1, team2 = teams[game['team1']] , teams[game['team2']]

Thanks in advance - and apologies if this is a lack of competence more than an issue. New to python, if not to coding in general.

jayb commented 6 years ago

Hey @nixonth,

Thanks for the heads-up.

There was a period of time over the weekend where some of the games in the 2017 schedule had an empty team, and this was causing the error. For example, after the Patriots won, but before they knew their opponent in the next game.

I updated the way we create the csv so that won't happen in the future.

nixonth commented 6 years ago

Hi Jay,

Thanks for taking a look at this so quickly. I was sure it was due to those missing lines, but I still could not for the life of me find a way to fix it. Working now! Love the work you all do.

Tim

On Jan 16, 2018, at 10:14 AM, Jay Boice notifications@github.com<mailto:notifications@github.com> wrote:

Hey @nixonthhttps://github.com/nixonth,

Thanks for the heads-up.

There was a period of time over the weekend where some of the games in the 2017 schedule had an empty team, and this was causing the error. For example, after the Patriots won, but before they knew their opponent in the next game.

I updated the way we create the csv so that won't happen in the future.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/fivethirtyeight/nfl-elo-game/issues/4#issuecomment-357991293, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AhxJYDtT5v7XB1ZXWdV6CXnNd7R5xJ17ks5tLLzUgaJpZM4RddTe.