# Float indexer to numpy array

575
January 05, 2017, at 08:30 AM

I want to make a numpy array that takes a float as a indexes (like, for example, a Pandas Series would if it had a float-type index). So let me explain. I want something like:

``````i = np.array([(5.,1), (6.,2)], dtype=[('foo', 'f4'),('bar', 'i4')])
``````

if the first type (the 'foo' type), could be used as a indexer. So I could do

``````print(i[:5.5])
``````

and it would print `1`, or `(5., 1)`.

I'm pretty sure that's possible with Numpy, I just don't know how to.

You might need logical indexing:

``````i[i['foo'] < 5.5]
# array([(5.0, 1)],
#       dtype=[('foo', '<f4'), ('bar', '<i4')])
``````

`i[:5.5]` does not have an obvious meaning.

`i[:5]` means the first 5 elements of the array (or list). `i[:6]` the first 6. What is it supposed to do with the `5.5`? `floor(5.5)`? Ceiling? Return 5 and half elements? 5 elements plus a linear interpolation between the 5th and 6th?

Actually `:5.5` works (in 1.11) but with a warning:

``````In [346]: np.arange(10)[:5]
Out[346]: array([0, 1, 2, 3, 4])
In [347]: np.arange(10)[:5.5]
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
#!/usr/bin/python3
Out[347]: array([0, 1, 2, 3, 4])
``````

With your structured array, record indexing works the same way:

``````In [349]: i = np.array([(5.,1), (6.,2)], dtype=[('foo', 'f4'),('bar', 'i4')])
In [350]: i[:5]
``````

It's ok to slice beyond the end - it just returns everything.

``````Out[350]:
array([(5.0, 1), (6.0, 2)],
dtype=[('foo', '<f4'), ('bar', '<i4')])
In [351]: i[:5.5]
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
#!/usr/bin/python3
Out[351]:
array([(5.0, 1), (6.0, 2)],
dtype=[('foo', '<f4'), ('bar', '<i4')])
``````

================

Indexing in `numpy` is just a way of counting. There isn't an indexing or labeling list or array. That's something that `pandas` has added, but it isn't part of `numpy`. In your `i` array, fields do have names, e.g. `i['foo']`. It can look like column labeling, but it's dangerous to confuse structured fields with 2d columns.

POPULAR ONLINE

### cannot import name Shrinkage

I'm running a python script which uses Shrinkage class from pyRiemann packageEven though that I've installed this package, I got this error:

295

295

### Python 3.4: Not Implemented Error

I am learning the TensorFlow from the example at the website: https://githubcom/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/autoencoder

844