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
#2
Can you add the traceback?
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply
#3
(Apr-24-2020, 01:40 PM)Jeff900 Wrote: Can you add the traceback?

I have never heard of a traceback with IndexError???? Are you sure this applies to this case?
Reply
#4
A traceback is what you get as response when an error occurred. Like this:

Output:
>>> 1 + a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>>
It gives a bit more info then just the error.
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply
#5
(Apr-24-2020, 02:52 PM)Jeff900 Wrote: A traceback is what you get as response when an error occurred. Like this:

Output:
>>> 1 + a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>>
It gives a bit more info then just the error.

Like I said Jeff, your suggestion dosen't apply here.
The only available traceback is:
----> 5     t1_seed = df_seeds[(df_seeds.TeamID == t1) & (df_seeds.Season == year)].seed_int.values[0]
and the following:
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
 
Reply
#6
I later figured this out myself..most of these issues just require persistence and hard work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 1,950 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 739 May-15-2023, 03:28 PM
Last Post: buran
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,961 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,844 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,279 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,409 May-03-2022, 01:39 PM
Last Post: Anldra12
  For loop index out of bounds armitron121 2 2,617 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,232 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  IndexError: list index out of range rf_kartal 6 2,761 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