这是什么意思,我该如何解决?

时间:2019-08-12 23:34:18

标签: python jupyter-notebook jupyter

我正在尝试为将来的异常创建多维数组。该错误不断弹出。有什么问题?我以前在更大的数组上使用了相同的代码,试图缩短它,现在却出现了很多错误。

import numpy as np
import csv

    emptystns=[]
    with open('stationlist2.tsv') as i:
    j = csv.reader(i, delimiter='\t')
    next (j)
    for r in j:
        emptystns.append(r[0]) 



    lines = []

    ns = sum(1 for line in open('stationlist2.tsv'))-1
        #3660 stations (minus the headerline which is not a stationid)
    nmons = 12
        #12 months in a year
    nlays = 8
        #the number of depths in layers is the number of layers
    stnslist= []

    data=np.ma.masked_all((ns, nlays, nmons), dtype=np.float64)  

    with open ('SoilAverage1981.tsv', 'r') as f:
    reader= csv.reader(f,delimiter = '\t')
    next(reader)

    for line in reader:
        temp = line[2:]
        istn= emptystns.index(line[0])
        ilayers = line[1]

        for i, info in enumerate(temp):                         
            imonth = i
            if info !='':
                data[istn, ilayers, imonth] = info

print 'done'

错误消息:

IndexError: only integers, slices (`:`), ellipsis (`...`),     numpy.newaxis (`None`) and integer or boolean arrays are valid indices

1 个答案:

答案 0 :(得分:0)

我会做一个有根据的猜测,因为似乎只有一个地方可以索引一个numpy数组:

data[istn, ilayers, imonth]

查看data,检查其shapedtype

查看索引istnilayersimonth。错误提供的列表中是否没有?可能是字符串,可能是浮点数或列表?

integers, 
slices (`:`), 
ellipsis (`...`),     
numpy.newaxis (`None`) 
integer or boolean arrays 

调试此类错误的基础:

  • 确定错误发生的地方

  • 标识表达式中的所有变量

  • 验证这些变量的标识和/或属性

  • 哪些人与错误消息相匹配或与记录的行为不相符。

相关问题