[Numpy-discussion] who owns the data?

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Nov 30 15:30:47 EST 2011

just a basic question (since I haven't looked at this in some time)

I'm creating a structured array in a function. However, I want to
return the array with just a simple dtype

uni = uni.view(dt).reshape(-1, ncols)
return uni

the returned uni has owndata=False. Who owns the data, since the
underlying, original array went out of scope?


uni = np.asarray(uni, dt).reshape(-1, ncols)
return uni

looks obvious but raises exception

uni.dtype = dt
uni.reshape(-1, ncols)
return uni

this works and uni owns the data. I'm only worried whether assigning
to dtype directly is not a dangerous thing to do.

>>> u
array([0, 0, 0, 1, 1, 0, 1, 1])
>>> u.dtype = np.dtype("float")
>>> u
array([  0.00000000e+000,   2.12199579e-314,   4.94065646e-324,

adding a safety check:

        for t in uni.dtype.fields.values():
            assert (t[0] == dt)

maybe I shouldn't care if nobody owns the data.



More information about the NumPy-Discussion mailing list