python - numpy structured arrays: help understanding output -
i trying learn how use numpy's structured arrays. specifically, trying add information more 1 field @ time. tried:
import numpy np numrec = np.zeros(8, dtype=[('col0', 'int16'), ('col1', 'int16'), ('col2', 'int16'), ('col3', 'int16')]) numrec[['col1','col2']][0:2] = [(3,5), (1,8)] print numrec
the above not work. values not added columns specified. surprising do not error when run it. can please explain happening?
thanks.
you setting values on temporary.
numrec[["col1", "col2"]]
returns copy of array. can see owndata flag.
>>> numrec[["col1", "col2"]].flags["owndata"] true
when index numpy array list, numpy returns copy of data. has copy, because, in general, list may not resolve regular, ordered view of underlying data. (this holds numpy array, not structured arrays.)
compare
>>> numrec[["col1"]].flags["owndata"] true >>> numrec["col1"].flags["owndata"] false
also, if numpy array view, base member holds underlying array.
>>> id(numrec["col1"].base) == id(numrec) true
Comments
Post a Comment