To add a field to a structured numpy array, it is quite simply to create a new array with a new dtype, copy over the old fields, and add the new field. However, I need to do this for an array that takes a lot of memory, and I would rather not duplicate all of it. Both my own implementation and the (slow) implementation in numpy.lib.recfunctions.append_fields
duplicate memory.
Is there a way to add a field to a structured ndarray
, without duplicating memory? That means, either a way that avoids creating a new ndarray
, or a way to create a new ndarray
that points to the same data as the old?
Solutions that do duplicate RAM:
There is a similar question where the challenge is to remove, not add, fields. The solution uses a view, which should work for a subset of the original data, but I'm not sure if it can be amended when I rather want to add fields.