There are references to using np.append
to add to an initially empty array, such as How to add a new row to an empty numpy array.
Instead, my question is how to allocate extra empty space at the end of an array so that it can later be assigned to.
An example:
# Inefficient: The data in new_rows gets copied twice.
array = np.arange(6).reshape(2, 3)
new_rows = np.square(array)
new = np.concatenate((array, new_rows), axis=0)
# Instead, we would like something like the following:
def append_new_empty_rows(array, num_rows):
new_rows = np.empty_like(array, shape=(num_rows, array.shape[1]))
return np.concatenate((array, new_rows), axis=0)
array = np.arange(6).reshape(2, 3)
new = append_new_empty_rows(array, 2)
np.square(array[:2], out=new[2:])
However, the np.concatenate()
likely still copies the empty data array?
Is there something like an np.append_empty()
?