# Extract unmasked elements from Astropy Table

**URL:** <https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145>\
**Category:** Astropy\
**Tags:** astropy, table, question\
**Created:** [September 8, 2021, 3:02am UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145 "2021-09-08T03:02:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelle](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/kelle/32/39_2.png) [@kelle](https://community.openastronomy.org/u/kelle)\
**Post date:** [September 8, 2021, 3:02am UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145/1 "2021-09-08T03:02:03Z")

</div>

Let’s kick the tires of this forum!

I’m trying to extract the non-masked rows from an Astropy Table. From the [Astropy Table documentation](https://docs.astropy.org/en/stable/table/masking.html#mask), I can get the indices of the masked, but how do I get the inverse?

> To get the indices of masked elements, use an expression like:  
> `>>> t['a'].mask.nonzero()`

I’ve tried  
`nans = t['a'].mask.nonzero()`  
`unmasked_t = t['a'][-nans]`

But that didn’t work.  
`bad operand type for unary -: 'tuple'`

Any pointers?

Thanks!  
Kelle

---

<div class="post-metadata">

**Author:** ![Cadair](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/cadair/32/422_2.png) [@Cadair](https://community.openastronomy.org/u/Cadair)\
**Post date:** [September 8, 2021, 8:56am UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145/2 "2021-09-08T08:56:09Z")

</div>

I think (without testing it) you could use `np.logical_not` on the mask before the `nonzero` call.

I.e.

```auto
unmasked = np.logical_not(t['a'].mask).nonzero()

```

---

<div class="post-metadata">

**Author:** ![kelle](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/kelle/32/39_2.png) [@kelle](https://community.openastronomy.org/u/kelle)\
**Post date:** [September 8, 2021, 2:09pm UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145/3 "2021-09-08T14:09:52Z")

</div>

it works!

any thoughts on this approach vs using Pandas `.notna()` ?

---

<div class="post-metadata">

**Author:** ![nstarman](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/nstarman/32/41_2.png) [@nstarman](https://community.openastronomy.org/u/nstarman)\
**Post date:** [September 8, 2021, 7:40pm UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145/4 "2021-09-08T19:40:39Z")

</div>

Isn’t the mask a NumPy boolean array? then the non-masked elements are just `notmasked = t['a'][~t['a'].mask]`  
Or do you want the indices of the rows?

---

<div class="post-metadata">

**Author:** ![nstarman](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/nstarman/32/41_2.png) [@nstarman](https://community.openastronomy.org/u/nstarman)\
**Post date:** [December 15, 2021, 11:09pm UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145/5 "2021-12-15T23:09:14Z")

</div>

Addendum: `~` can sometimes be finicky. I’ve taken to using `not` instead, even if it’s a Boolean array.

---

<div class="post-metadata">

**Author:** ![saimn](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/saimn/32/40_2.png) [@saimn](https://community.openastronomy.org/u/saimn)\
**Post date:** [December 16, 2021, 8:51am UTC](https://community.openastronomy.org/t/extract-unmasked-elements-from-astropy-table/145/6 "2021-12-16T08:51:12Z")

</div>

I think what you want is `t[~t['a'].mask]` to get a new table where rows with masked elements in `'a'` have been removed:

```auto
In [29]: t
Out[29]: 
<Table masked=True length=2>
  a b  
int64 int64
----- -----
    1 --
   -- 4

In [30]: t[~t['a'].mask]
Out[30]: 
<Table masked=True length=1>
  a b  
int64 int64
----- -----
    1 --

```

Pandas has powerful ways to do selection on columns, but I’m not sure there is really a point in reimplementing that in Astropy. In Astropy columns are simple Numpy arrays, so most simple things can be in a slightly more verbose way (compared to Pandas) with simple Numpy selections. And for more advanced usage it’s easy to convert the table to Pandas (`t.to_pandas()`).
