I try to convert a list of astropy Table in a numpy array of astropy Table. In first instance I tried np.asarray(list)
and np.array(list)
but the astropy table inside the list were converted with the list as numpy ndarray.
Example :
t = Table({'a': [1,2,3], 'b':[4,5,6]})
t2 = Table({'a': [7,8,9], 'b':[10,11,12]})
mylist = [t1, t2]
print(mylist)
The output is:
[<Table length=3>
a b
int64 int64
----- -----
1 4
2 5
3 6,
<Table length=3>
a b
int64 int64
----- -----
7 10
8 11
9 12]
Then if I apply np.array()
the output is :
array([[(1, 4), (2, 5), (3, 6)],
[(7, 10), (8, 11), (9, 12)]], dtype=[('a', '<i8'), ('b', '<i8')])
but I want the following:
array([<Table length=3>
a b
int64 int64
----- -----
1 4
2 5
3 6,
<Table length=3>
a b
int64 int64
----- -----
7 10
8 11
9 12])
My actual solution is :
if isinstance(mylist, list):
myarray = np.empty(len(mylist), dtype='object')
for i in range(len(myarray)):
myarray[i] = mylist[i]
else:
myarray = mylist
return myarray
It works but I was thinking that there is maybe something built-in in numpy to do this, but I can't find it.