I am doing some simulations in experimental cosmology, and encountered this problem while working with numpy arrays. I'm new to numpy, so I am not sure if I'm doing this wrong or if it's a bug. I run:
Enthought Python Distribution -- www.enthought.com
Version: 7.3-1 (32-bit)
Python 2.7.3 |EPD 7.3-1 (32-bit)| (default, Apr 12 2012, 11:28:34)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "credits", "demo" or "enthought" for more information.
>>> import numpy as np
>>> t = np.arange(10)
>>> t[t < 8][t < 5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many boolean indices
>>>
I expected it to return:
array([0, 1, 2, 3, 4])
since t[t < 8] should presumably be treated as just another ndarray?
The numpy documentation (http://docs.scipy.org/doc/numpy/user/basics.indexing.html) says about boolean arrays as indices:
As with index arrays, what is returned is a copy of the data, not a view as one gets with slices.
running type(t[t < 8])
also gives ndarray
, which I guess should have all the properties of a numpy array. Should I perhaps do this better with list expressions? I have not done a timed comparison yet, but I would imagine this to be a problem for large 2D arrays.