在 astropy Table 中删除带有屏蔽元素的行

Drop rows with masked elements in astropy Table

考虑 astropy Table() object.

from astropy.io import ascii

weather_data = """
  day,precip,type
  ,1.5,rain
  Tues, 9.,
  Wed,1.1,snow
  ed,,aaa
  Wd,1.1,snow
  """

dat = ascii.read(weather_data)
print(dat)

day  precip type
---- ------ ----
  --    1.5 rain
Tues    9.0   --
 Wed    1.1 snow
  ed     --  aaa
  Wd    1.1 snow

我需要删除包含至少一个屏蔽元素的所有行。最终的 table 应该如下所示:

day  precip type
---- ------ ----
 Wed    1.1 snow
  Wd    1.1 snow

这似乎是一项简单的任务,但我在 docs 中找不到完成它的方法。

添加

我知道我可以使用 .to_pandas() 将 table 转换为 pandas 对象,然后使用 .dropna() 之类的东西,但这迫使我必须 pandas 安装了我不想要的。

对于通用的方式

import operator
# if python3: from functools import reduce
dat[reduce(operator.and_, [~dat[col].mask for col in dat.columns])]

day   precip type
str4 float64 str4
---- ------- ----
 Wed     1.1 snow
  Wd     1.1 snow

这个过程所做的只是一种通用的做法

dat[(~dat['day'].mask) & (~dat['precip'].mask) & (~dat['type'].mask)]

这是另一种方式:

dat.remove_rows(np.where([c.data for c in dat.mask.itercols()])[-1])

但我同意你的看法,这种对无效值的过滤应该由屏蔽 table 支持。