[Numpy-discussion] Database with Nulls to Numpy Structure
Christopher Barker
Chris.Barker@noaa....
Fri Oct 2 22:38:55 CDT 2009
Mark Janikas wrote:
> Thanks for the input! I wonder if I can resize my own record array? I.e. one call to truncate... Ill give it a go.
you should be able too, yes. Be careful though, you can't call resize()
if there are any other references to the array.
> But the resize works great as it doesn't make a copy:
Actually, it's not that simple. With numpy arrays, there is the array
object itself, and there is the data block that the array points to. Whn
you call resize() it may make a copy of the data block (which is why it
won't work if there are other references to it), while keeping the same
python object.
> In [12]: a = NUM.arange(10)
>
> In [13]: id(a)
> Out[13]: 190182896
>
> In [14]: a.resize(5,)
>
> In [15]: a
> Out[15]: array([0, 1, 2, 3, 4])
>
> In [16]: id(a)
> Out[16]: 190182896
So this shows you have the same python object. I think there is a way to
get the value of the pointer to the data block, but I dont' know off the
top of my head how.
> Whereas the slice seems to make a copy/reassign:
>
> In [18]: a = a[0:2]
>
> In [19]: id(a)
> Out[19]: 189981184
slicing creates a new python object, but it doesn't copy the actual data:
In [4]: b = a[2:5]
In [5]: a is b
Out[5]: False
In [6]: a[2:5] = 10
In [7]: a
Out[7]: array([ 0, 1, 10, 10, 10, 5, 6, 7, 8, 9])
In [8]: b
Out[8]: array([10, 10, 10])
so you can see a and b are different python objects, but they share the
same data block.
HTH,
-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@noaa.gov
More information about the NumPy-Discussion
mailing list