与Pandas套餐对接#

这个 pandas package是一个用于表状结构的高性能数据分析的包,它是对 Table 班在 astropy .

为了在两个服务器之间交换数据 Table 类和 pandas.DataFrame 类(中的主要数据结构 pandas )、 Table 类包括两个方法, to_pandas()from_pandas()

基本示例#

为了演示,我们可以创建一个最小表:

>>> from astropy.table import Table
>>> t = Table()
>>> t['a'] = [1, 2, 3, 4]
>>> t['b'] = ['a', 'b', 'c', 'd']

然后,我们可以将其转换为 DataFrame **

>>> df = t.to_pandas()
>>> df
   a  b
0  1  a
1  2  b
2  3  c
3  4  d
>>> type(df)
<class 'pandas.core.frame.DataFrame'>

还可以从 DataFrame **

>>> t2 = Table.from_pandas(df)
>>> t2
<Table length=4>
  a      b
int64 string8
----- -------
    1       a
    2       b
    3       c
    4       d

细节#

往返的转换 pandas 注意事项如下:

  • 这个 DataFrame 结构不支持多维列,因此 Table 不能将具有多维列的对象转换为 DataFrame

  • 蒙面桌子可以转换,但熊猫使用哨兵,例如 numpy.nan 以表示缺失的值。Astropy使用单独的屏蔽数组来表示屏蔽值,其中“屏蔽下”的值被保留。当将任何星形屏蔽列转换为pandas DataFrame时,原始值将丢失。

  • 表与 混合柱Time, SkyCoord, and Quantity can be converted, but with loss of information or fidelity. For instance, Time columns will be converted to a pandas TimeSeries ,但该对象只有64位精度,不支持闰秒或时间刻度。

下面在一个更复杂的示例中强调了这些问题,该示例的表包含掩蔽列和混合列。

首先,我们创建一个具有掩蔽列和mixin列的表::

>>> import numpy as np
>>> from astropy.table import MaskedColumn, QTable
>>> from astropy.time import Time
>>> from astropy.coordinates import SkyCoord
>>> import astropy.units as u
>>> t = QTable()
>>> t['a'] = MaskedColumn([1, 2, 3], mask=[False, True, False])
>>> t['b'] = MaskedColumn([1.0, 2.0, 3.0], mask=[False, False, True])
>>> t['c'] = MaskedColumn(["a", "b", "c"], mask=[True, False, False])
>>> t['tm'] = Time(["2021-01-01", "2021-01-02", "2021-01-03"])
>>> t['sc'] = SkyCoord(ra=[1, 2, 3] * u.deg, dec=[4, 5, 6] * u.deg)
>>> t['q'] = [1, 2, 3] * u.m

>>> t
<QTable length=3>
  a      b     c              tm              sc       q
                                           deg,deg     m
int64 float64 str1           Time          SkyCoord float64
----- ------- ---- ----------------------- -------- -------
    1     1.0   -- 2021-01-01 00:00:00.000  1.0,4.0     1.0
   --     2.0    b 2021-01-02 00:00:00.000  2.0,5.0     2.0
    3      --    c 2021-01-03 00:00:00.000  3.0,6.0     3.0

现在我们将此表转换为 DataFrame

>>> df = t.to_pandas()
>>> df
      a    b    c         tm  sc.ra  sc.dec    q
0     1  1.0  NaN 2021-01-01    1.0     4.0  1.0
1  <NA>  2.0    b 2021-01-02    2.0     5.0  2.0
2     3  NaN    c 2021-01-03    3.0     6.0  3.0

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 7 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   a       2 non-null      Int64
 1   b       2 non-null      float64
 2   c       2 non-null      object
 3   tm      3 non-null      datetime64[ns]
 4   sc.ra   3 non-null      float64
 5   sc.dec  3 non-null      float64
 6   q       3 non-null      float64
dtypes: Int64(1), datetime64[ns](1), float64(4), object(1)
memory usage: 303.0+ bytes

注意几件事:

  • 原始表中的屏蔽值将被pandas中的哨兵值替换。整列 a 被转换为可空的integer列,而字符串列 c 被转换成 object

  • Time 对象转换为pandas TimeSeries,使用 datetime64[ns] .

  • SkyCoord 对象转换为两个浮动列 sc.rasc.dec ,并且单位丢失了。

  • Quantity 对象被转换为浮动列并且单位丢失。

现在转换回表格::

>>> t_df = QTable.from_pandas(df)
>>> t_df
<QTable length=3>
  a      b     c              tm            sc.ra   sc.dec    q
int64 float64 str1           Time          float64 float64 float64
----- ------- ---- ----------------------- ------- ------- -------
    1     1.0   -- 2021-01-01T00:00:00.000     1.0     4.0     1.0
   --     2.0    b 2021-01-02T00:00:00.000     2.0     5.0     2.0
    3      --    c 2021-01-03T00:00:00.000     3.0     6.0     3.0

Time 列被恢复(受前面讨论的限制),但 SkyCoordQuantity 列不会恢复到原始表中的状态。

最后,在往返转换中,原始表中的掩码值被替换为零或“”:

# Original data values
>>> for nm in 'a', 'b', 'c':
...     print(t[nm].data.data)
[1 2 3]
[1. 2. 3.]
['a' 'b' 'c']

# Data values after round-trip conversion
>>> for nm in 'a', 'b', 'c':
...     print(t_df[nm].data.data)
[1 0 3]
[ 1.  2. nan]
['' 'b' 'c']