Christopher Freeman
Christopher Freeman

Reputation: 11

Getting: 'IndexError: index 1 is out of bounds for axis 0 with size 0' but can't tell why

I get an index error when trying to fill values to my array, but I can't see why this error would occur.

    Hist = HistData[:pos, :]
    
    if Hist.shape[0] != 0:
        for y in range(2012, 2019):
            hist_pos = 0
            YearHist = np.zeros((150000, 1))
            for k in range(Hist.shape[0]):
                tm = time.gmtime(Hist[k, 0])
                if tm.tm_year == y:
                    YearHist[hist_pos, 0] = Hist[k, vartype]
                    hist_pos+=1
                
                YearHist = YearHist[:hist_pos]

This is the snippet of offending code. This is set into code that had run just fine previously. The object "Hist" is an Nx11 2d array. N is the number of non-zero entries taken from a larger data structure. Sometimes it can be zero, but I ignore those for the purposes of setting up the yearly array.

When I run the code I get:

Traceback (most recent call last):

File "YearlyHistoPlots.py", line 34, in

 YearHist[hist_pos, 0] = Hist[k, vartype]

IndexError: index 1 is out of bounds for axis 0 with size 0

Which confuses me. I explicitly define YearHist to have axis 0 with size 150k and anytime Hist has axis zero with size zero this line of code shouldn't execute. Anything I'm missing?

Upvotes: 1

Views: 1125

Answers (1)

Duda
Duda

Reputation: 3746

The problem lies in the last line, you accidently put it in the for-k-Loop, but it belongs to the for-y loop. Delete one Tab and you should be fine.

                YearHist = YearHist[:hist_pos]

becomes

            YearHist = YearHist[:hist_pos]

Upvotes: 0

Related Questions