astropy/astropy — pull request #1583 by mhvk (2013-10-15 05:31:53+00:00)
Title: Trial: Table subclassed from quantity

While looking again at #1540, I realised that to have table columns behave like quantities, it is not good enough to just add some methods, as, e.g., `c.mean()` should produce a quantity, not just a number. One more complete solution is to subclass. Fortunately, with `super` this is surprisingly easy, as shown in this PR. Note especially the lovely definition
```
class QuantityColumn(Column, Quantity):
pass
```
Some notes:
* This also includes #1558 as well as an unfinished but mostly working `MaskedQuantity` implementation.
* For now, it is mostly to open this for comments, as some parts do not yet work (e.g., `table.join`, etc.).
* The API has an oddity in that assignment to `unit` only changes the nature of the `Column` if it is already part of a `Table` (where I use `_rebuild_table_column_views`). I don't know how to handle a column by itself.
* Also unclear is what should be done upon repeat assignment to `unit` (#1253 rears its head!).
* I'm not completely sure how useful the non-Quantity `Column` still will be; for numerical cases, a `QuantityColumn` with `unit=''` would always seem more appropriate.

Anyway, for now it is fun being able to do things like
```
In [1]: from astropy.table import Table; import astropy.units as u; import numpy as np

In [2]: t = Table([[1.,2., 3.],[4., 5., 6.]], names=('a','b'))

In [3]: t['a'].unit = 'rad'

In [4]: t['a'].to(u.deg)
Out[4]:

In [5]: t['a']**2
Out[5]:

In [6]: np.cos(t['a'])
Out[6]:

In [7]: t['a']/u.s
Out[7]:

In [8]: t['c'] = t['a']/u.s

In [9]: t.columns['c']
Out[13]:

array([ 1., 2., 3.])
```