Search code examples
pythonarraysnumpyuser-defined-types

Named dtype array: Difference between a[0]['name'] and a['name'][0]?


I came across the following oddity in numpy which may or may not be a bug:

import numpy as np
dt = np.dtype([('tuple', (int, 2))])
a = np.zeros(3, dt)
type(a['tuple'][0])  # ndarray
type(a[0]['tuple'])  # ndarray

a['tuple'][0] = (1,2)  # ok
a[0]['tuple'] = (1,2)  # ValueError: shape-mismatch on array construction

I would have expected that both of the options below work. Opinions?


Solution

  • This was an upstream bug, fixed as of NumPy PR #5947, with a fix in 1.9.3.