0

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.

1
  • you want float slicing it seems. Commented Jan 4, 2017 at 23:29

2 Answers 2

2

You might need logical indexing:

i[i['foo'] < 5.5]

# array([(5.0, 1)], 
#       dtype=[('foo', '<f4'), ('bar', '<i4')])
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.