Programmers learn & share
0 votes
275 views

I am trying to create a rating data frame for some football teams. To rate the teams I am using elo rating system  :

https://en.wikipedia.org/wiki/Elo_rating_system

My data frame consists of pre-match ratings of home and away teams and it gets updated after every match.

My data frame code is:

for index in df.index:

    counter = counter + 1

    if df.at[index,'updated'] == 2: 

        try:
            all_home_fixtures = df.date_rank[df['localteam_id'] == df.at[index,'localteam_id']]
            next_home_fixture =all_home_fixtures[all_home_fixtures>df.at[index,'date_rank']].min()
            next_home_index = df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])].index.item()
        except ValueError:
            print('ERROR 1 at' + str(index))
            df.at[index,'updated'] = 4

        try:
            all_away_fixtures = df.date_rank[df['visitorteam_id'] == df.at[index,'visitorteam_id']]
            next_away_fixture = all_away_fixtures[all_away_fixtures > df.at[index,'date_rank']].min()
            next_away_index = df[(df['date_rank'] == next_away_fixture) & (df['visitorteam_id'] == df.at[index,'visitorteam_id'])].index.item()
        except ValueError:
            print('ERROR 2 at' + str(index))
            df.at[index,'updated'] = 4

str(df.at[index,'date_rank']) + '; Next home date rank: ' + str(df.at[next_home_index,'date_rank']) + '; Next away date rank: ' + str(df.at[next_away_index,'date_rank']))

        df.at[next_home_index, 'home_elo'] = update_elo(df.at[index,'home_elo'],df.at[index,'away_elo'],df.at[index,'actual_score'])
        df.at[next_away_index, 'away_elo'] = update_elo(df.at[index,'away_elo'],df.at[index,'home_elo'],1 - df.at[index,'actual_score']) # Swap function inputs for away team


        df.at[next_home_index, 'updated'] = df.at[next_home_index, 'updated'] + 1
        df.at[next_away_index, 'updated'] = df.at[next_away_index, 'updated'] + 1

        df.at[index,'updated'] = 3

But after working in some of the rows it gives me an error like :

ValueError: can only convert an array of size 1 to a python scalar

Can any one help me to find what the problem really is and help me to solve the problem.

by (5.7k points)  
edited by | 275 views

1 Answer

0 votes

Please check if all your indexes are unique or not. Some of your index calls may return multiple values if you have any index that is working variously.

Try this in your code:

df.index.nunique() == df.index.shape[0]

 Your pd.Series.item may require one item in the Series to return a scalar.

If a series is with length zero it will return a ValueError.

Try:

df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])]

 

by (32.3k points)  
2,218 questions
2,683 answers
59 comments
241 users