[SciPy-dev] How to handle a[...] in numpy?
Sasha
ndarray at mac.com
Sat Jan 7 22:10:03 CST 2006
In Numeric a[...] would return an array unless a was 0-rank and a
python type othewise. What is the right way to do the same in numpy?
The problem is that in numpy subscripting always fails on 0-rank arrays:
>>> array(0)[...]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: 0-d arrays can't be indexed.
Note that in numpy I would like to get numpy scalars rather than
python type from a 0-rank array.
A related question and a proposal:
Queston: Why do ascalar() and item() return python types rather than
numpy scalar types?
Proposal: Although I like a lot that 0-rank arrays and numpy scalar
types non-iterable, it may be reasonable to allow a[...]. This way
ellipsis can be interpereted as any number of ":"s including zero.
Another subscript operation that makes sense for scalars would be
a[...,newaxis] or even a[{newaxis, }* ..., {newaxis,}*], where
{newaxis,}* stands for any number of comma-separated newaxis tokens.
This will allow one to use ellipsis in generic code that would work on
any numpy type. I will contribute code if there is any interest.
-- sasha
PS: I've just realized that ellipsis does include "zero number of
':'s", only not for zero rank objects:
>>> array([0,0])[...,:]
array([0, 0])
More information about the Scipy-dev
mailing list