• Register
0 votes
1k 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.

10 7 2
6,050 points

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'])]

 

11 6 4
34,930 points

Related questions

1 vote
1 answer 35 views
35 views
Problem: I am doing a online course on python. I got an assginement from there. So I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return ... .int(a) TypeError: only size-1 arrays can be converted to Python scalars Do anyone know why this error is happening, please help.
asked Apr 3 LizzyM 6.1k points
0 votes
1 answer 254 views
254 views
I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return numpy.int(a) a = numpy.arange(3, 13.5, 0.7) matplotlib.pyplot.plot(a, ... 4, in ThisFunction return numpy.int(a) TypeError: only size-1 arrays can be converted to Python scalars how can I solve this problem?
asked Mar 15 LizzyM 6.1k points
2 votes
2 answers 232 views
232 views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6 samhaz 5k points