Skip to content Skip to sidebar Skip to footer

Array Slicing Raises Indexerror: Too Many Indices For Array

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? Code: >>> ty

Solution 1:

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

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 can only handle one time at a time:

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]')

Solution 2:

I cannot reshape the data and need to parse it the good old fashion way:

import numpy as np
import h5py

h5data = h5py.File("test.h5", 'r')
log = h5data['/log']
time = np.array(
        [
            datetime.utcfromtimestamp(
                    float("{0}.{1:06.0f}".format(*x))) for x in log['time']
        ],
     dtype=np.datetime64)
ook = np.array([x[2] for x in log], dtype=float)

This sucks. ☹


Post a Comment for "Array Slicing Raises Indexerror: Too Many Indices For Array"