In [16]:
from astropy.table import Table, Column, hstack
import numpy as np

Table manipulation


In [28]:
arr = np.arange(15).reshape(5, 3)
print(arr)
t = Table(arr, names=('a', 'b', 'c'), meta={'keywords': {'key1': 'val1'}})
print(t)


aa = Column(np.arange(5), name='aa')
print(aa)

t.add_column(aa, index=0)  # Insert before the first table column
print(t)
bb = Column(np.arange(5))
t.add_column(bb, name='bb')  # Append unnamed column to the table with 'bb' as name
print(t)

t2 = Table(np.arange(25).reshape(5, 5), names=('e', 'f', 'g', 'h', 'i'))
print(t2)
t.add_columns(t2.columns.values())
print(t)
print(t2)


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]
 [12 13 14]]
 a   b   c 
--- --- ---
  0   1   2
  3   4   5
  6   7   8
  9  10  11
 12  13  14
 aa
---
  0
  1
  2
  3
  4
 aa  a   b   c 
--- --- --- ---
  0   0   1   2
  1   3   4   5
  2   6   7   8
  3   9  10  11
  4  12  13  14
 aa  a   b   c   bb
--- --- --- --- ---
  0   0   1   2   0
  1   3   4   5   1
  2   6   7   8   2
  3   9  10  11   3
  4  12  13  14   4
 e   f   g   h   i 
--- --- --- --- ---
  0   1   2   3   4
  5   6   7   8   9
 10  11  12  13  14
 15  16  17  18  19
 20  21  22  23  24
 aa  a   b   c   bb  e   f   g   h   i 
--- --- --- --- --- --- --- --- --- ---
  0   0   1   2   0   0   1   2   3   4
  1   3   4   5   1   5   6   7   8   9
  2   6   7   8   2  10  11  12  13  14
  3   9  10  11   3  15  16  17  18  19
  4  12  13  14   4  20  21  22  23  24
 e   f   g   h   i 
--- --- --- --- ---
  0   1   2   3   4
  5   6   7   8   9
 10  11  12  13  14
 15  16  17  18  19
 20  21  22  23  24

In [2]:
t2.columns['e', 'f'].values()


Out[2]:
[<Column name='e' dtype='int64' length=5>
  0
  5
 10
 15
 20, <Column name='f' dtype='int64' length=5>
  1
  6
 11
 16
 21]

In [3]:
x = t['a', 'b', 'c']
x


Out[3]:
Table length=5
abc
int64int64int64
012
345
678
91011
121314

In [4]:
t


Out[4]:
Table length=5
aaabcbbefghi
int64int64int64int64int64int64int64int64int64int64
0012001234
1345156789
267821011121314
39101131516171819
412131442021222324

In [5]:
y = t.remove_column('a')
y

In [6]:
t


Out[6]:
Table length=5
aabcbbefghi
int64int64int64int64int64int64int64int64int64
012001234
145156789
27821011121314
3101131516171819
4131442021222324

In [7]:
x = t

In [8]:
x


Out[8]:
Table length=5
aabcbbefghi
int64int64int64int64int64int64int64int64int64
012001234
145156789
27821011121314
3101131516171819
4131442021222324

In [9]:
t.remove_column('f')

In [10]:
x


Out[10]:
Table length=5
aabcbbeghi
int64int64int64int64int64int64int64int64
01200234
14515789
278210121314
31011315171819
41314420222324

In [11]:
y = x[[True, False, True, False, True]]

In [12]:
y


Out[12]:
Table length=3
aabcbbeghi
int64int64int64int64int64int64int64int64
01200234
278210121314
41314420222324

In [13]:
x


Out[13]:
Table length=5
aabcbbeghi
int64int64int64int64int64int64int64int64
01200234
14515789
278210121314
31011315171819
41314420222324

In [14]:
t1 = Table.read("""a   b    c
                   1   foo  1.4
                   2   bar  2.1
                   3   baz  2.8""", format='ascii')
t2 = Table.read("""d     e
                   ham   eggs
                   spam  toast""", format='ascii')

t3 = Table.read("""a    b
                   M45  2012-02-03""", format='ascii')

In [19]:
x = hstack([t1, t2, t3])
print(x)


a_1 b_1  c   d     e   a_3    b_3    
--- --- --- ---- ----- --- ----------
  1 foo 1.4  ham  eggs M45 2012-02-03
  2 bar 2.1 spam toast  --         --
  3 baz 2.8   --    --  --         --

Masked array


In [21]:
import numpy.ma as ma

In [22]:
x = np.array([1, 2, 3, -1, 5])

In [23]:
mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])
mx


Out[23]:
masked_array(data=[1, 2, 3, --, 5],
             mask=[False, False, False,  True, False],
       fill_value=999999)

In [24]:
mx.data


Out[24]:
array([ 1,  2,  3, -1,  5])

In [25]:
np.mean(mx.data)


Out[25]:
2.0

In [26]:
np.mean(mx)


Out[26]:
2.75

In [27]:
print(mx)
print(x)


[1 2 3 -- 5]
[ 1  2  3 -1  5]

In [ ]: