[Numpy-discussion] scalar recordarrays
Matthew Koichi Grimes
mkg@cs.nyu....
Sat Mar 17 17:53:03 CDT 2007
I've found that if I have a record array of shape [] (i.e. a scalar
recarray), I can't set its fields.
>>> recarr
recarray((0.0, 0.0, 0.0),
dtype=[('x', '<f8'), ('dx', '<f8'), ('delta', '<f8')])
>>> recarr.x[...] = 1.0
TypeError: object does not support item assignment
In the above, recarr.x returned a float "0.0", then attempted the
assignment "0.0 = 1.0", which raised the TypeError. This is in contrast
with the behavior of scalar ndarrays, where the above works as expected:
>>> arr
array(0.0)
>>> arr[...] = 1.0
>>> arr
array(1.0)
I this a bug, or is there some reason for this behavior? The obvious
workaround is reshape a scalar to a size-1 array before setting .x:
>>> if recarr.ndim == 0:
....: recarr.reshape([1]).x[...] = 1.0 else: recarr.x[...] = 1.0
....: else:
....: recarr.x[...] = 1.0
But this is cumbersome and easy to forget to do.
-- Matt
More information about the Numpy-discussion
mailing list