Skip to content

Nested compound types TypeError #1197

@ckoerber

Description

@ckoerber

First of all, thank you for helping with the h5py package.

I believe I have encountered a bug related to nested compound data types. Below you can find a minimal example.

# create compound data
d = np.dtype((('i', 2), 3))
a = np.arange(2*3).reshape((3,2))
data = np.array([tuple(a)], dtype=d)
# a.shape = (1, 3, 2)

# create dataset
with h5py.File("test.h5") as fle:
    dset = fle.create_dataset(
        "test", 
        shape=[1,], 
        dtype=d,
    )
    # dset.dtype = dtype((('<i4', (2,)), (3,)))
    dset[...] = data

The last line raises the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-14e95409766a> in <module>()
     13     )
     14     # dset.dtype = dtype((('<i4', (2,)), (3,)))
---> 15     dset[...] = data

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

/usr/local/lib/python3.7/site-packages/h5py/_hl/dataset.py in __setitem__(self, args, val)
    613                 raise TypeError(
    614                     "When writing to array types, last N dimensions have to match (got %s, but should be %s)"
--> 615                     % (valshp, shp)
    616                 )
    617             mtype = h5t.py_create(numpy.dtype((val.dtype, shp)))

TypeError: When writing to array types, last N dimensions have to match (got (2,), but should be (3,))

Transposing the format d or the array a does not help.

Instead, if I create the dataset with a different type, the write works without problems

dset = new_file.create_dataset(
    "test", 
    shape=[1,], 
    dtype=np.dtype(('i', (3, 2))),
)

My version info is as follows

Summary of the h5py configuration
---------------------------------

h5py    2.9.0
HDF5    1.10.4
Python  3.7.2 (default, Jan 13 2019, 12:50:01) 
[Clang 10.0.0 (clang-1000.11.45.5)]
sys.platform    darwin
sys.maxsize     9223372036854775807
numpy   1.16.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions