Python 对于大小为 0 的轴 0,索引 0 超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29214017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
index 0 is out of bounds for axis 0 with size 0
提问by johnhenry
I am filling two arrays, field_in_k_space_REAL
and field_in_k_space_IMAGINARY
, with values extracted from a Gaussian distribution, paying attention to respect the symmetry to get a real field when I inverse-transform the arrays. Here is the code:
我正在填充两个数组,field_in_k_space_REAL
并field_in_k_space_IMAGINARY
使用从高斯分布中提取的值,注意在对数组进行逆变换时尊重对称性以获得实场。这是代码:
field_in_k_space_REAL = zeros(n, float)
field_in_k_space_IMAGINARY = zeros(n, float)
field_in_k_space_REAL[0] = 0.0
for i in range(1, int(n/2+1)):
field_in_k_space_REAL[i] = np.random.normal(mu, math.sqrt((1/2)*math.exp(-(2*math.pi*i*sigma/L)*(2*math.pi*i*sigma/L))))
x = range(int(n/2+1), int(n))
y = range(1, int(n/2))
zipped = zip(x, y)
for j, j2 in zipped:
field_in_k_space_REAL[j] = field_in_k_space_REAL[j-2*j2]
field_in_k_space_IMAGINARY[0] = 0.0
for i in range(1, int(n/2)):
field_in_k_space_IMAGINARY[i] = np.random.normal(mu, math.sqrt((1/2)*math.exp(-(2*math.pi*i*sigma/L)*(2*math.pi*i*sigma/L))))
field_in_k_space_IMAGINARY[n/2] = 0.0
for j, j2 in zipped:
field_in_k_space_IMAGINARY[j] = - field_in_k_space_IMAGINARY[j-2*j2]
print 'field_k', field_in_k_space_REAL
But I keep having the following error:
但我一直有以下错误:
field_in_k_space_REAL[0] = 0.0
IndexError: index 0 is out of bounds for axis 0 with size 0
Can someone explain why and how to fix it?
有人可以解释为什么以及如何解决它吗?
采纳答案by James Kelleher
My guess is that the array field_in_k_space_REAL
is actually of length 0, most likely because you set n = 0
further up in your code (do you use n
in a loop maybe?). I can reproduce the error when I directly initialize an array of length 0.
我的猜测是该数组field_in_k_space_REAL
实际上长度为 0,很可能是因为您n = 0
在代码中进行了进一步设置(您是否n
在循环中使用?)。当我直接初始化长度为 0 的数组时,我可以重现该错误。
回答by Jansora
in fact, you'd better use:
事实上,你最好使用:
field_in_k_space_REAL.loc[index] = 0.0
instead of:
代替:
field_in_k_space_REAL[index] = 0.0
回答by Akshay Salvi
this is because of NAN value in your close column use np.isfinite. For example :
这是因为您的关闭列中的 NAN 值使用np.isfinite。例如 :
btt_ohlc[np.isfinite(btt_ohlc['Close'])]