这里发生的是,当你这样做时:
ts = np.array(t, dtype=dt)
dtype 被应用于table
. 它适用于前 11 个元素,然后它得到'RIGHT'
它不能变成一个整数。这是没有它的情况'RIGHT'
(这会很混乱!):
>>> t[:2,:-1]
array([['0.00', '0.00', '5.751E-01', '-2.08', '9.532E-05', '-86.19', '1.7442', '-73.8670', '1.7442', '0.0002', '0.00'],
['2.00', '0.00', '5.747E-01', '-2.11', '1.291E-04', '-82.47', '1.7390', '-71.2312', '1.7390', '0.0002', '0.00']],
dtype='|S9')
>>> np.array(t[:2,:-1], dt)
array([[(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00'),
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00'),
(0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, 0.5751, '5.751'),
(-2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, -2.08, '-2.08'),
(9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, 9.532e-05, '9.532'),
(-86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, -86.19, '-86.1'),
(1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, '1.744'),
(-73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, -73.867, '-73.8'),
(1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, 1.7442, '1.744'),
(0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, '0.000'),
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00')],
[(2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, '2.00'),
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00'),
(0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, 0.5747, '5.747'),
(-2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, -2.11, '-2.11'),
(0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, 0.0001291, '1.291'),
(-82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, -82.47, '-82.4'),
(1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, '1.739'),
(-71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, -71.2312, '-71.2'),
(1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, 1.739, '1.739'),
(0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, '0.000'),
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, '0.00')]],
dtype=[('LOCATION_THETA', '<f8'), ('LOCATION_PHI', '<f8'), ('ETHETA_MAGN', '<f8'), ('ETHETA_PHASE', '<f8'), ('EPHI_MAGN', '<f8'), ('EPHI_PHASE', '<f8'), ('DIRECTIVITY_VERT', '<f8'), ('DIRECTIVITY_HORIZ', '<f8'), ('DIRECTIVITY_TOTAL', '<f8'), ('POLARISATION_AXIALR', '<f8'), ('POLARISATION_ANGLE', '<f8'), ('POLARISATION_DIRECTION', 'S5')])
因此,您可以看到,对于每个元素,您都会得到一个带有 dtype 的漂亮的小元组('record')datatype1
(它甚至使最后一个为您的字符串)。
有几种方法可以解决这个问题,最好的方法是从一开始就使用正确的 dtype 创建/导入数组,这样您就不必复制它。对于某些转换,可以在view
其中简单地解释数据,就好像它具有新的复杂 dtype 一样,但这不会将字符串转换为数字,因为这比假装数据是数字更复杂。
在您的情况下,您可能应该使用比常规结构化数组recarray
稍微复杂的 a ,然后您可以使用该函数。它需要一个列列表,每个列都有统一的类型,而不是行,因此转置:fromarrays
>>> np.rec.fromarrays(t.T, dt)
rec.array([ (0.0, 0.0, 0.5751, -2.08, 9.532e-05, -86.19, 1.7442, -73.867, 1.7442, 0.0002, 0.0, 'RIGHT'),
(2.0, 0.0, 0.5747, -2.11, 0.0001291, -82.47, 1.739, -71.2312, 1.739, 0.0002, 0.0, 'RIGHT'),
(4.0, 0.0, 0.5738, -2.21, 0.0001632, -80.31, 1.7243, -69.1973, 1.7243, 0.0003, 0.0, 'RIGHT'),
(6.0, 0.0, 0.5722, -2.38, 0.0001973, -78.94, 1.7001, -67.5479, 1.7001, 0.0003, 0.0, 'RIGHT'),
(8.0, 0.0, 0.5699, -2.61, 0.0002314, -78.02, 1.6663, -66.1644, 1.6663, 0.0004, 0.01, 'RIGHT')],
dtype=[('LOCATION_THETA', '<f8'), ('LOCATION_PHI', '<f8'), ('ETHETA_MAGN', '<f8'), ('ETHETA_PHASE', '<f8'), ('EPHI_MAGN', '<f8'), ('EPHI_PHASE', '<f8'), ('DIRECTIVITY_VERT', '<f8'), ('DIRECTIVITY_HORIZ', '<f8'), ('DIRECTIVITY_TOTAL', '<f8'), ('POLARISATION_AXIALR', '<f8'), ('POLARISATION_ANGLE', '<f8'), ('POLARISATION_DIRECTION', 'S5')])
迷人的!但是等等,现在是这个rec.array
......如果你想保持这种状态,那很好。如果您希望它是一个常规的结构化数组,请执行以下操作:
>>> np.asarray(np.rec.fromarrays(t.T, dt))