数组切片会引发IndexError:数组索引过多 [英] Array slicing raises IndexError: too many indices for array

查看:79
本文介绍了数组切片会引发IndexError:数组索引过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据(来自HDF5文件),我只想获取一些列.我试图切片数组,但得到了IndexError,我无法理解为什么. 有什么想法吗?

I have some data (from an HDF5 file) which I want to get only some columns. I tried slicing the array but got an IndexError and I cannot fathom why. Any idea?

代码:

>>> type(x)
numpy.ndarray
>>> x
array([((1537445457, 517647), 0.45301986, 13.807418, 0.10681067, 6.856901 , 2.8895614, 15.341972, 2.8160472, 5.7942305, 67.95573, 2.5007493, 13.925896, 1.4587704, 6.1075644, 68.347534, 2.6885383, 15.334871, 0.31526774, 5.9454284, 0.713507  , nan, nan, nan, 0., 0., 0., 0., 0.),                                                                                                        
       ((1537445457, 630955), 0.5777189 , 13.807683, 0.10421388, 6.8743234, 2.7194788, 14.866684, 2.753199 , 5.7411118, 68.38666, 3.0199409, 14.754977, 1.4933671, 5.7361865, 67.82245 , 3.4682775, 15.384485, 0.3508615 , 6.3675985, 0.31907487, nan, nan, nan, 0., 0., 0., 0., 0.)],                                                                                                       
      dtype=[('time', [('sec', '<u4'), ('usec', '<u4')]), ('0', '<f4'), ('1', '<f4'), ('2', '<f4'), ('3', '<f4'), ('4', '<f4'), ('5', '<f4'), ('6', '<f4'), ('7', '<f4'), ('8', '<f4'), ('9', '<f4'), ('10', '<f4'), ('11', '<f4'), ('12', '<f4'), ('13', '<f4'), ('14', '<f4'), ('15', '<f4'), ('16', '<f4'), ('17', '<f4'), ('18', '<f4'), ('19', '<f4'), ('20', '<f4'), ('21', '<f4'), ('22', '<f4'), ('23', '<f4'), ('24', '<f4'), ('25', '<f4'), ('26', '<f4')])

>>> x[0, [0, 1]]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-123-41118eda084a> in <module>()
----> 1 x[0, [0, 1]]

IndexError: too many indices for array

推荐答案

您的数组是结构化的数组,带有复合dtype.您的hdf5负载没有问题.

Your array is a structured one, with a compound dtype. There's nothing wrong with your hdf5 load.

In [135]: arr.shape
Out[135]: (2,)
In [136]: arr.dtype
Out[136]: dtype([('time', [('sec', '<u4'), ('usec', '<u4')]), ('0', '<f4'), ('1', '<f4'), ('2', '<f4'), ('3', '<f4'), ('4', '<f4'), ('5', '<f4'), ('6', '<f4'), ('7', '<f4'), ('8', '<f4'), ('9', '<f4'), ('10', '<f4'), ('11', '<f4'), ('12', '<f4'), ('13', '<f4'), ('14', '<f4'), ('15', '<f4'), ('16', '<f4'), ('17', '<f4'), ('18', '<f4'), ('19', '<f4'), ('20', '<f4'), ('21', '<f4'), ('22', '<f4'), ('23', '<f4'), ('24', '<f4'), ('25', '<f4'), ('26', '<f4')])
In [137]: len(arr.dtype.names)
Out[137]: 28

它有2条记录.每条记录包含28个字段

It has 2 records. Each record contains 28 fields

In [138]: arr.dtype.names
Out[138]: 
('time',
 '0',
 '1',
 '2',
 '3',
 ....

第一个字段时间"本身是复合的:

The first field, 'time', is itself compound:

In [139]: arr['time']
Out[139]: 
array([(1537445457, 517647), (1537445457, 630955)],
      dtype=[('sec', '<u4'), ('usec', '<u4')])

字段是按名称而不是列号"引用的

fields are referenced by name, not 'column number'

在列表压缩方法中,您对记录进行迭代,然后按数字访问记录的元素:

In your list compression approach you iterate on records, and then access the elements of the record by number:

In [148]: np.array([x[2] for x in arr])
Out[148]: array([13.807418, 13.807683], dtype=float32)
In [149]: arr['1']
Out[149]: array([13.807418, 13.807683], dtype=float32)

时间解析可能仍需要记录迭代:

The time parsing may still need record iteration:

In [152]: time = np.array(
     ...:      [
     ...:          np.datetime64(
     ...:              datetime.utcfromtimestamp(
     ...:                  float("{0}.{1:06.0f}".format(x[0][0], x[0][1]))))
     ...:          for x in arr
     ...:      ],
     ...:      dtype=np.datetime64)
     ...:      
In [153]: 
In [153]: time
Out[153]: 
array(['2018-09-20T12:10:57.517647', '2018-09-20T12:10:57.630955'],
      dtype='datetime64[us]')

datetime一次只能处理一次:

In [176]: np.array(
     ...:      [datetime.utcfromtimestamp(
     ...:                  float("{0}.{1:06.0f}".format(*x)))
     ...:          for x in arr['time']
     ...:      ],dtype=np.datetime64)
     ...:      
Out[176]: 
array(['2018-09-20T12:10:57.517647', '2018-09-20T12:10:57.630955'],
      dtype='datetime64[us]')

这篇关于数组切片会引发IndexError:数组索引过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆