1

I have a numpy table

<Table length=3>
  a     b  
int64 int64
----- -----
    1     3
    2     5
    4     7

And I would like to convert a row to a numpy array. But when I try, I end up with an array with no dimensions

In: np.array(mytable[0]).shape
Out: ()

and if I do

myrow = mytable[0]
myrow[0]

I get the error

IndexError: too many indices for array

Is there something like t[0].values I could do, that would return array([1, 3]) ?

2 Answers 2

2

The Table.Row object provides an iterator over the values, so you could do:

>>> np.array(list(t[0]))
array([1, 3])
Sign up to request clarification or add additional context in comments.

Comments

1

When you slice a row from a table in Astropy and convert to an ndarray, you get a 0D structured array back, which is the shape attribute is empty. For a general solution, numpy provides a structured_to_unstructured method that will work well for more than just a single row slice as well.


>>> np.lib.recfunctions.structured_to_unstructured(np.array(t[0]))
array([1, 3])

>>> np.lib.recfunctions.structured_to_unstructured(np.array(t[1:]))
array([[2, 5],
       [4, 7]])

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.