Programmers learn & share
0 votes

I am trying to create a rating data frame for some football teams. To rate the teams I am using 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[index,'updated'] == 2: 

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

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

str([index,'date_rank']) + '; Next home date rank: ' + str([next_home_index,'date_rank']) + '; Next away date rank: ' + str([next_away_index,'date_rank']))[next_home_index, 'home_elo'] = update_elo([index,'home_elo'],[index,'away_elo'],[index,'actual_score'])[next_away_index, 'away_elo'] = update_elo([index,'away_elo'],[index,'home_elo'],1 -[index,'actual_score']) # Swap function inputs for away team
[next_home_index, 'updated'] =[next_home_index, 'updated'] + 1[next_away_index, 'updated'] =[next_away_index, 'updated'] + 1[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.


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


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