0

Why does the snipped below give:

"TypeError: Array objects cannot currently deal with void, unicode or object arrays"?

Python 3.8.2, tables 3.6.1, numpy 1.19.1

import numpy as np
import tables as tb
TYPE = np.dtype([
    ('d', 'f4')
])
with tb.open_file(r'c:\temp\file.h5', mode="a") as h5file:
    h5file.create_group(h5file.root, 'grp')
    arr = np.array([(1.1)], dtype=TYPE)
    h5file.create_array('/grp', str('arr'), arr)
Gary
  • 1
  • 2
  • The `dtype` is wrong. I think `h5py` will work with this dtype, but evidently oytables has not inplemented it. Did you read pytables docs? – hpaulj Aug 17 '20 at 08:32
  • h5py does not install in my Pycharm venv with many installation problems. I read the docs and numpy arrays are supposed to be supported. I am not sure what is the difference between numpy array with or without dtype in this context. – Gary Aug 17 '20 at 08:40
  • Arrays in general may be supported, but not all dtypes. h5py can't save object dtypes. You have a structured array with a compound dtype (the void in the error massage) – hpaulj Aug 17 '20 at 09:24
  • Thanks hpaulj, In that case I will convert it to straight ndarray before saving and apply the dtype after reading it. – Gary Aug 17 '20 at 13:26

1 Answers1

0

File.create_array() is for homogeneous dtypes (all ints, or all floats, etc). PyTables uses a different object to save mixed dytpes. You need to use File.create_table() instead. See modified code below (only the last line changed).

TYPE = np.dtype([ ('d', 'f4') ])
with tb.open_file(r'c:\temp\file.h5', mode="a") as h5file:
    h5file.create_group(h5file.root, 'grp')
    arr = np.array([(1.1)], dtype=TYPE)
    h5file.create_table('/grp', str('arr'), arr)

Note: you will get an error with mode='a' if you run with an existing temp.h5 file from your previous work. This is due to a conflict with group /grp created the first time.

kcw78
  • 7,131
  • 3
  • 12
  • 44