[AstroPy] Indexing table rows

Tom Aldcroft aldcroft@head.cfa.harvard....
Fri Mar 1 06:56:27 CST 2013


I forgot to mention that an alternate workaround is to use boolean masking:

>>> t = Table(np.reshape(np.arange(12),(4,3)))
>>> ok = t['col0'] > 5
>>> print t[ok]
col0 col1 col2
---- ---- ----
   6    7    8
   9   10   11

Cheers,
Tom

On Fri, Mar 1, 2013 at 7:08 AM, Tom Aldcroft
<aldcroft@head.cfa.harvard.edu> wrote:
> Hi David,
>
> Thanks for reporting this issue.  This is a bug and is now tracked at
>   https://github.com/astropy/astropy/issues/838
>
> This should be fixed in the 0.2.1 release scheduled for early April.
>
> Cheers,
> Tom
>
> On Fri, Mar 1, 2013 at 5:59 AM, David Buscher <dfb@mrao.cam.ac.uk> wrote:
>> 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
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy@scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
>>


More information about the AstroPy mailing list