[Numpy-discussion] Subcripts for arrays of nested structures

Robert Radocinski rgr001@sbcglobal....
Sun May 10 12:08:57 CDT 2009


In order to illustrate my question which involves two related subscript 
expressions, on arrays of nested structures,  I have created a short 
code example which is given below.

In the code example, I create a nested "structure" data type (called 
dtype3) and two numpy.ndarray's (called arecords and brecords) which are 
identical in shape and dtype.  The remaining code  consists of the 
replacement statement (lines 34 and 49) at the heart of my question and 
print statements.

Specifically, my question centers on my expectation that the statement  
on line 34 (arecords["data"][1] = data_values) would have the same 
effect on arecords as the statement  on line 49 
(brecords[1]["data"]=data_values) would have on brecords.  From running 
the code example, this is obviously not the case.  In the former case, 
all four of the "data" arrays of the second record in arecords are set 
to the values found in data_values.  In the latter case, only the first 
"data" array of the second record in brecords is set to a value in 
data_values and the other three "data" values remain unchanged.

I am baffled by the latter case involving brecords.  If you examine the 
left-hand side and righthand side of the statement on line 49 
(brecords[1]["data"]=data_values),  both sides have the same shape(1-dim 
with 4 elements) and dtype.  I am having a great deal of difficulty 
trying to understand why the replacement statement only effects the 
first "data" array and not all 4 "data" arrays.  What am I overlooking?

Any help in explaining this behavior would be appreciated.

Thanks,
RR



CODE EXAMPLE 
----------------------------------------------------------------------------

import numpy
type1 = numpy.dtype([("a", numpy.int32),
                     ("b", numpy.int32)])
type2 = numpy.dtype([("alpha", numpy.float64),
                     ("beta", numpy.float64),
                     ("gamma", numpy.float64)])
type3 = numpy.dtype([("header", type1),
                     ("data", type2, 4)])

header_values = numpy.empty(1, dtype=type1)
data_values = numpy.empty(4, dtype=type2)
header_values["a"] = 1000
header_values["b"] = 2000
data_values["alpha"] = [11.0, 21.0, 31.0, 41.0]
data_values["beta"]  = [12.0, 22.0, 32.0, 42.0]
data_values["gamma"] = [13.0, 23.0, 33.0, 43.0]

arecords = numpy.empty(2, dtype=type3)
brecords = numpy.empty(2, dtype=type3)

print
print "Case A:"
print
print arecords, ': arecords:'.upper()
print data_values, ': data_values'.upper()
print arecords["data"][1].shape, ': arecords["data"][1].shape'.upper()
print arecords[1]["data"].shape, ': arecords[1]["data"].shape'.upper()
print data_values.shape, ': data_values.shape'
print arecords["data"][1].dtype, ': arecords["data"][1].dtype'.upper()
print arecords[1]["data"].dtype, ': arecords[1]["data"].dtype'.upper()
print data_values.dtype, ': data_values.dtype'.upper()
arecords["header"][1] = header_values
arecords["data"][1] = data_values
print arecords, ': arecords:'.upper()

print
print "Case B:"
print
print brecords, ': brecords:'.upper()
print data_values, ': data_values'.upper()
print brecords["data"][1].shape, ': brecords["data"][1].shape'.upper()
print brecords[1]["data"].shape, ': brecords[1]["data"].shape'.upper()
print data_values.shape, ': data_values.shape'
print brecords["data"][1].dtype, ': brecords["data"][1].dtype'.upper()
print brecords[1]["data"].dtype, ': brecords[1]["data"].dtype'.upper()
print data_values.dtype, ': data_values.dtype'.upper()
brecords[1]["header"] = header_values
brecords[1]["data"] = data_values
print brecords, ': brecords:'.upper()



More information about the Numpy-discussion mailing list