[AstroPy] Indexing table rows

David Buscher dfb@mrao.cam.ac...
Fri Mar 1 04:59:11 CST 2013


It appears that the astropy.tables module does not accept the same row
indexing syntax as the corresponding ndarray, particularly the syntax
output by np.where(), which is a tuple containing an ndarray. Example
below:

Python 2.7.3 |EPD 7.3-1 (64-bit)| (default, Apr 12 2012, 11:14:05)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "credits", "demo" or "enthought" for more information.
>>> import numpy as np
>>> from astropy.table import Table

>>> t=Table(np.reshape(np.arange(12),(4,3)))
>>> print t
col0 col1 col2
---- ---- ----
   0    1    2
   3    4    5
   6    7    8
   9   10   11

>>> rows=np.where(t["col0"]>5)
>>> print rows
(array([2, 3]),)

# ndarray version
>>> print np.array(t)[rows]
[(6, 7, 8) (9, 10, 11)]

# Tables version
>>> print t[rows[0]]
col0 col1 col2
---- ---- ----
   6    7    8
   9   10   11

>>> print t[rows]
ERROR: TypeError: unhashable type: 'numpy.ndarray' [astropy.table.table]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/astropy/table/table.py",
line 1282, in __getitem__
    if any(x not in set(self.colnames) for x in item):
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/astropy/table/table.py",
line 1282, in <genexpr>
    if any(x not in set(self.colnames) for x in item):
TypeError: unhashable type: 'numpy.ndarray'

I am using astropy version 0.2. Is this behaviour on purpose or a bug?
It can be worked around by taking the first element of the indexing
tuple as above, but it seems counterintuitive that the output of
np.where() cannot be used directly to select table rows.

David


More information about the AstroPy mailing list