[Numpy-discussion] who owns the data?

josef.pktd@gmai... josef.pktd@gmai...
Wed Nov 30 14:30:47 CST 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?

alternatives

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

looks obvious but raises exception

2)
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,
         2.12199579e-314])

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.

Thanks,

Josef


More information about the NumPy-Discussion mailing list