Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting astropy.table.columns to a numpy array

I'd like to plot points:

points = np.random.multivariate_normal(mean=(0,0), cov=[[0.4,9],[9,10]],size=int(1e4))

print(points)
  [[-2.50584156  2.77190372]
   [ 2.68192136 -3.83203819]
   ..., 
   [-1.10738221 -1.72058301]
   [ 3.75168017  5.6905342 ]]

print(type(points))
  <class 'numpy.ndarray'>

data = ascii.read(datafile)  
type(data['ra'])
  astropy.table.column.Column
type(data['dec'])
 astropy.table.column.Column

and then I try:

points = np.array([data['ra']], [data['dec']])

and get a

TypeError: data type not understood

Thoughts?

like image 745
npross Avatar asked Dec 02 '25 07:12

npross


1 Answers

An astropy Table Column object can be converted to a numpy array using the data attribute:

In [7]: c = Column([1, 2, 3])

In [8]: c.data
Out[8]: array([1, 2, 3])

You can also convert an entire table to a numpy structured array with the as_array() Table method (e.g. data.as_array() in your example).

BTW I think the actual problem is not about astropy Column but your numpy array creation statement. It should probably be:

arr = np.array([data['ra'], data['dec']])

This works with Column objects.

like image 100
Tom Aldcroft Avatar answered Dec 04 '25 21:12

Tom Aldcroft