I am trying to copy a recarray and change the names of the fields/records in the new array. However, this modifies the names of the original array (the values are not unlinked, however). Example:
import numpy as np
import copy
define original array
arr = np.array(np.random.random((3,2)),
dtype=[('a','float'),('b','float')])
first copy
arr2 = arr.copy()
arr2.dtype.names = ('c','d')
arr.dtype.names
--> ('c','d')
second copy
arr3 = copy.deepcopy(arr2)
arr2.dtype.names = ('e','f')
arr.dtype.names
--> ('e','f')
Why does this happen and how to keep this from happening? I suspect the dtype
is a separate list/object whose reference is copied upon copy()
, but even if I assign a deep copy of the dtype
object to the original array, I get the same result:
dt = copy.deepcopy(arr.dtype)
arr.dtype = dt
arr3.dtype.names = ('g','h')
arr.dtype.names
--> ('g','h')