Astropy 表:设置来自另一个 table 的数据类型
Astropy Tables: set data type from another table
我有一个 astropy.Table
,称它为 T
:
a b c d
int32 float64 str1 float64
----- ------- ---- -------
1 2.0 0 10.0
4 5.0 1 20.0
5 8.5 2 30.0
我想从 numpy 数组创建另一个 table,但具有与 T
相同的列和数据类型
import numpy as np
A = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8]])
我可以用相同的列名创建一个 table
S = Table(A, names=T.colnames)
但是如果我也尝试传递数据类型
S = Table(A, names=T.colnames, dtype=T.dtype)
然后我得到一个错误 ValueError: dtype must be a list or None
,而 list(T.dtype)
只是 returns TypeError: 'numpy.dtype' object is not iterable
如何将数据类型从一种 table 传递到另一种?
您可以使用当前版本的 astropy 执行此操作:
Table(A, names=T.colnames, dtype=[T[name].dtype for name in T.colnames])
这已经在 astropy 的 master 分支中得到改进,在 astropy 的下一个版本 4.2 中你可以简单地做:
Table(A, dtype=T.dtype)
我有一个 astropy.Table
,称它为 T
:
a b c d
int32 float64 str1 float64
----- ------- ---- -------
1 2.0 0 10.0
4 5.0 1 20.0
5 8.5 2 30.0
我想从 numpy 数组创建另一个 table,但具有与 T
import numpy as np
A = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8]])
我可以用相同的列名创建一个 table
S = Table(A, names=T.colnames)
但是如果我也尝试传递数据类型
S = Table(A, names=T.colnames, dtype=T.dtype)
然后我得到一个错误 ValueError: dtype must be a list or None
,而 list(T.dtype)
只是 returns TypeError: 'numpy.dtype' object is not iterable
如何将数据类型从一种 table 传递到另一种?
您可以使用当前版本的 astropy 执行此操作:
Table(A, names=T.colnames, dtype=[T[name].dtype for name in T.colnames])
这已经在 astropy 的 master 分支中得到改进,在 astropy 的下一个版本 4.2 中你可以简单地做:
Table(A, dtype=T.dtype)