[Numpy-discussion] Can I index array starting with 1?

Stéfan van der Walt stefan at sun.ac.za
Thu Jul 28 18:42:54 EDT 2011


Hi Jeremy

On Thu, Jul 28, 2011 at 3:19 PM, Jeremy Conlin <jlconlin at gmail.com> wrote:
> I have a need to index my array(s) starting with a 1 instead of a 0.
> The reason for this is to be consistent with the documentation of a
> format I'm accessing. I know I can just '-1' from what the
> documentation says, but that can get cumbersome.
>
> Is there a magic flag I can pass to a numpy array (maybe when it is
> created) so I can index starting at 1 instead of the Python default?

You may want to have a look at some of the labeled array packages out
there, such as larry, datarray, pandas, etc.  I'm sure at least one of
them allows integer re-labelling, although I'm not certain whether it
can be done in a programmatic fashion.

An alternative may be to create an indexing function that remaps the
input space, e.g.:

def ix(n):
    return n - 1

x[ix(3), ix(5)]

But that looks pretty nasty, and you'll have to expand ix quite a bit
to handle slices, etc. :/

Stéfan



More information about the NumPy-Discussion mailing list