Python Forum
IndexError: index 0 is out of bounds for axis 0 with size 0 error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: index 0 is out of bounds for axis 0 with size 0 error
#1
Good day fellow coders,

I am a little stuck with an index error issue. Below is the description:

Table name: Sub
ID, Pred
2014_1107_1110, 0.5
2014_1107_1112, 0.5
2014_1107_1113, 0.5
2014_1107_1124, 0.5

Season
Season, Seed, TeamID
1998, W01, 3330
1998, W02, 3163
1998, W03, 3112
1998, W04, 3301


Method to simplfy the dataset

def seed_to_int(seed):
    s_int = int(seed[1:3])
    return s_int
df_seeds['seed_int'] = df_seeds.Seed.apply(seed_to_int)
df_seeds.drop(labels = ['Seed'], inplace = True, axis = 1)
df_seeds.head()


[b]Output from simplification method

Season TeamID seed_int
0 1998 3330 1
1 1998 3163 2
2 1998 3112 3
3 1998 3301 4
4 1998 3272 5


Pandas encaps SeedsTable
df_seeds = pd.read_csv(data_directory + '/Seeds.csv')
output
Season Seed TeamID
0 1998 W01 3330
1 1998 W02 3163
2 1998 W03 3112
3 1998 W04 3301
4 1998 W05 3272

Code that generates index Error

X_test = np.zeros(shape = (n_test_games, 1))

for ii, row in df_sample_sub.iterrows():
    year, t1, t2 = get_year_t1_t2(row.ID)
    t1_seed = df_seeds[(df_seeds.TeamID == t1) & (df_seeds.Season == year)].seed_int.values[0]
    t2_seed = df_seeds[(df_seeds.TeamID == t2) & (df_seeds.Season == year)].seed_int.values[0]
    diff_seed = t1_seed - t2_seed
    X_test[ii,0] = diff_seed
Actual index Error output
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-37-0215f157ee60> in <module>
      3 for ii, row in df_sample_sub.iterrows():
      4     year, t1, t2 = get_year_t1_t2(row.ID)
----> 5     t1_seed = df_seeds[(df_seeds.TeamID == t1) & (df_seeds.Season == year)].seed_int.values[0]
      6     t2_seed = df_seeds[(df_seeds.TeamID == t2) & (df_seeds.Season == year)].seed_int.values[0]
      7     diff_seed = t1_seed - t2_seed

IndexError: index 0 is out of bounds for axis 0 with size 0
As you can see, the prolem is pointing to this line:
t1_seed = df_seeds[(df_seeds.TeamID == t1) & (df_seeds.Season == year)].seed_int.values[0]
Please can anyone proffer a solution to this?
Reply


Messages In This Thread
IndexError: index 0 is out of bounds for axis 0 with size 0 error - by tmhsa - Apr-24-2020, 10:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,661 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  pyscript index error while calling input from html form pyscript_dude 2 1,158 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 901 May-15-2023, 03:28 PM
Last Post: buran
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 7,254 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 2,143 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,600 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,572 May-03-2022, 01:39 PM
Last Post: Anldra12
  For loop index out of bounds armitron121 2 2,826 Feb-08-2022, 04:23 PM
Last Post: armitron121
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,705 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  IndexError: list index out of range rf_kartal 6 3,075 Sep-07-2021, 02:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020