Skip to content

BUG: structured array indexing dtype and dtype.descr changed in 1.14.0 #10387

@larsoner

Description

@larsoner

Running this code on 1.13.x and 1.14.0 yields different outputs:

import numpy as np
dt = [('a', int), ('b', float)]
data = np.zeros(1, dtype=dt)
data['a'] = 0
data['b'] = 1
data = data[['a']]
print(data.dtype)
print(data.dtype.descr)
np.save('test.npy', data)
data_read = np.load('test.npy')
print(data_read.dtype)
print(data_read.dtype.descr)

On 1.13.x the output is consistent -- for all four print statements it gives:

[('a', '<i8')]

On 1.14.x it changes to these, the first of which is now dict-like, the second (descr) has a new field ('', '|V8'), the third (round-trip IO) changes dtype back to a list, and the fourth (descr) changes the blank field name to '':

{'names':['a'], 'formats':['<i8'], 'offsets':[0], 'itemsize':16}
[('a', '<i8'), ('', '|V8')]
[('a', '<i8'), ('f1', 'V8')]
[('a', '<i8'), ('f1', '|V8')]

The real reason I found this bug is because the addition of the blank field name breaks scipy.io.savemat:

>>> from scipy import io  # on latest dev
>>> io.savemat('test.mat', dict(data=data))
  File "untitled0.py", line 16, in <module>
    io.savemat('test.mat', dict(data=data))
  File "scipy/io/matlab/mio.py", line 219, in savemat
    MW.put_variables(mdict)
  File "scipy/io/matlab/mio5.py", line 846, in put_variables
    self._matrix_writer.write_top(var, asbytes(name), is_global)
  File "scipy/io/matlab/mio5.py", line 587, in write_top
    self.write(arr)
  File "scipy/io/matlab/mio5.py", line 616, in write
    self.write_struct(narr)
  File "scipy/io/matlab/mio5.py", line 735, in write_struct
    self._write_items(arr)
  File "scipy/io/matlab/mio5.py", line 752, in _write_items
    self.write(el[f])
ValueError: no field of name 

If these structured dtype changes are intentional and not bugs, I can open a SciPy issue instead (which would either be to avoid writing void data, or write out unknown fields).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions