[Numpy-discussion] bug in deepcopy() of rank-zero arrays?
Chris Barker - NOAA Federal
chris.barker at noaa.gov
Fri Apr 19 11:55:39 EDT 2013
Hi folks,
In [264]: np.__version__
Out[264]: '1.7.0'
I just noticed that deep copying a rank-zero array yields a scalar --
probably not what we want.
In [242]: a1 = np.array(3)
In [243]: type(a1), a1
Out[243]: (numpy.ndarray, array(3))
In [244]: a2 = copy.deepcopy(a1)
In [245]: type(a2), a2
Out[245]: (numpy.int32, 3)
regular copy.copy() seems to work fine:
In [246]: a3 = copy.copy(a1)
In [247]: type(a3), a3
Out[247]: (numpy.ndarray, array(3))
Higher-rank arrays seem to work fine:
In [253]: a1 = np.array((3,4))
In [254]: type(a1), a1
Out[254]: (numpy.ndarray, array([3, 4]))
In [255]: a2 = copy.deepcopy(a1)
In [256]: type(a2), a2
Out[256]: (numpy.ndarray, array([3, 4]))
Array scalars seem to work fine as well:
In [257]: s1 = np.float32(3)
In [258]: s2 = copy.deepcopy(s1)
In [261]: type(s1), s1
Out[261]: (numpy.float32, 3.0)
In [262]: type(s2), s2
Out[262]: (numpy.float32, 3.0)
There are other ways to copy arrays, but in this case, I had a dict
with a bunch of arrays in it, and needed a deepcopy of the dict. I was
surprised to find that my rank-0 array got turned into a scalar.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list