屏蔽和缺失值#

这个 astropy.table package provides support for masking and missing values in a table by using the numpy.ma masked array 用于定义屏蔽列的包,并通过支持 混合柱 它们提供了掩饰。这允许以与处理标准(未屏蔽)表大致相同的方式来处理具有缺失或无效条目的表。熟悉以下内容是有用的 masked array documentation 在中使用掩码表时 astropy.table

简而言之,这个概念是定义一个布尔掩码,它反映列数据数组的结构。无论掩码值在哪里 True, the corresponding entry is considered to be missing or invalid. Operations involving column or row access and slicing are unchanged. The key difference is that arithmetic or reduction operations involving columns or column slices follow the rules for operations on masked arrays .

备注

约简操作,如 numpy.sum()numpy.mean() 遵循忽略屏蔽(无效)值的约定。这与浮点的行为不同 NaN ,其数组之和包括一个或多个 NaN's 将导致 NaN

有关详细信息,请参阅NumPy增强建议 2425 ,以及 26

创建表#

可通过多种方式创建遮罩表:

创建一个包含一个或多个列作为MaskedColumn对象的表

>>> from astropy.table import Table, Column, MaskedColumn
>>> a = MaskedColumn([1, 2], name='a', mask=[False, True], dtype='i4')
>>> b = Column([3, 4], name='b', dtype='i8')
>>> Table([a, b])
<Table length=2>
  a     b
int32 int64
----- -----
    1     3
   --     4

这个 MaskedColumn 是掩码模拟的 Column 类,并提供用于创建和操作掩码数据列的接口。这个 MaskedColumn 类继承自 numpy.ma.MaskedArray ,与 Column 它继承自 numpy.ndarray 。这种区别是这两种情况有不同类别的主要原因。

请注意,表输出中的掩码条目显示为 -- .

Create a table with one or more columns as a NumPy MaskedArray

>>> import numpy as np
>>> a = np.ma.array([1, 2])
>>> b = [3, 4]
>>> t = Table([a, b], names=('a', 'b'))

Create a table from list data containing numpy.ma.masked

你可以使用 numpy.ma.masked 常量指示屏蔽或无效数据:

>>> a = [1.0, np.ma.masked]
>>> b = [np.ma.masked, 'val']
>>> Table([a, b], names=('a', 'b'))
<Table length=2>
  a     b
float64 str3
------- ----
    1.0   --
    --  val

从嵌入的列表中进行初始化 numpy.ma.masked 元素的速度比使用 numpy.ma.array()MaskedColumn 因此,如果性能是一个问题,您应该尽可能使用后一种方法。

将MaskedColumn对象添加到现有表中

>>> t = Table([[1, 2]], names=['a'])
>>> b = MaskedColumn([3, 4], mask=[True, False])
>>> t['b'] = b

向现有表添加新行并指定掩码参数

>>> a = Column([1, 2], name='a')
>>> b = Column([3, 4], name='b')
>>> t = Table([a, b])
>>> t.add_row([3, 6], mask=[True, False])

Create a new table object and specify masked=True

如果 masked=True 则每一列都将被创建为 MaskedColumn ,并且新列将始终作为 MaskedColumn

>>> Table([(1, 2), (3, 4)], names=('a', 'b'), masked=True, dtype=('i4', 'i8'))
<Table masked=True length=2>
  a     b
int32 int64
----- -----
    1     3
    2     4

将现有表转换为屏蔽表

>>> t = Table([[1, 2], ['x', 'y']])  # standard (unmasked) table
>>> t = Table(t, masked=True, copy=False)  # convert to masked table

此操作将转换 ColumnMaskedColumn 并确保随后添加的任何列都被屏蔽。

表访问#

几乎所有访问和修改数据列、行和单个元素的标准方法也适用于屏蔽表。

然而,关于 Row 通过为表的单行编制索引而获得的对象。对于标准表,可以比较两个这样的行是否相等,但对于屏蔽表,此比较将产生异常::

>>> t[0] == t[1]
Traceback (most recent call last):
...
ValueError: Unable to compare rows for masked table due to numpy.ma bug

掩蔽和填充#

两个 TableMaskedColumn 类提供属性和方法,以支持对缺少或无效数据的表进行操作。

面具#

可以通过掩码查看列和 mask 属性:

>>> t = Table([(1, 2), (3, 4)], names=('a', 'b'), masked=True)
>>> t['a'].mask = [False, True]  # Modify column mask (boolean array)
>>> t['b'].mask = [True, False]  # Modify column mask (boolean array)
>>> print(t)
 a   b
--- ---
  1  --
 --   4

屏蔽的条目显示为 -- 打印表格时。您可以直接在列或表级别查看掩码:

>>> t['a'].mask
array([False,  True]...)

>>> t.mask
<Table length=2>
  a     b
 bool  bool
----- -----
False  True
 True False

要获取被屏蔽元素的索引,请使用如下表达式:

>>> t['a'].mask.nonzero()[0]  
array([1])

填满#

可以用指定的填充值替换被屏蔽的条目(即,缺失或无效)。填写一份 MaskedColumn 会产生一个 Column 。掩码表中的每一列都有一个 fill_value 属性,该属性指定该列的默认填充值。要执行实际的替换操作,请使用 filled() 方法被调用。这需要一个可选参数,该参数可以覆盖默认列 fill_value 属性。**

>>> t['a'].fill_value = -99
>>> t['b'].fill_value = 33

>>> print(t.filled())
 a   b
--- ---
  1  33
-99   4

>>> print(t['a'].filled())
 a
---
  1
-99

>>> print(t['a'].filled(999))
 a
---
  1
999

>>> print(t.filled(1000))
 a    b
---- ----
   1 1000
1000    4