Reputation: 103
I am working with astropy and I am at a point where I printed Data
and got the following result as my output:
[<MaskedColumn name='phot_bp_rp_excess_factor' dtype='float32' description='BP/RP excess factor' length=1>
4.678),
(<MaskedColumn name='phot_bp_rp_excess_factor' dtype='float32' description='BP/RP excess factor' length=0>],
(<MaskedColumn name='phot_bp_rp_excess_factor' dtype='float32' description='BP/RP excess factor' length=1>
9.876)]
So now I am trying to put "BP/RP excess factor" in a new list x
, so I tried:
x=[]
for i in data:
if bp > 0:
x.append(bp[-1]) # "[-1]" to get rid of text and just print out the numbers
#I am expecting [4.678,9.876]
But this gives me an error saying index -1 is out of bounds for axis 0 with size 0
. I know this problems exist because in my output above I have length=0
, but I dont know to write a code that ignores if "length=0"
and gives me a list of numbers for elements that have "length=1"
.
Upvotes: 0
Views: 55
Reputation: 48710
I'm not familiar with astropy, but filtering on the length of data
seems to work in my local testing:
from astropy.table import MaskedColumn
In [16]: columns = [
...: MaskedColumn(data=[1]),
...: MaskedColumn(data=[]),
...: MaskedColumn(data=[2,3])
...: ]
In [17]: not_empty = [col for col in columns if len(col.data) > 0]
In [18]: not_empty
Out[18]:
[<MaskedColumn dtype='int64' length=1>
1,
<MaskedColumn dtype='int64' length=2>
2
3]
In [19]: len(not_empty)
Out[19]: 2
Edit:
column.size
also works, thanks to @python_user in the comments:
not_empty = [col for col in columns if col.size > 0]
Upvotes: 1