Getting an array interface and using it
Hi, I have an object that exposes an array interface. I want to modify the data it contains, but using numpy.array(myObject) seems to copy the data and thus my object is not modified. Am I mistaken or did I make a mistake in my array interface ? Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher
Matthieu Brucher wrote:
I have an object that exposes an array interface. I want to modify the data it contains, but using numpy.array(myObject) seems to copy the data and thus my object is not modified. Am I mistaken or did I make a mistake in my array interface ?
I think numpy.array(object) always makes a copy. You want numpy.asarray(object) which will make a view if object exposes the array interface and matches the type and sizes requested. -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
2007/11/12, Christopher Barker <Chris.Barker@noaa.gov>:
Matthieu Brucher wrote:
I have an object that exposes an array interface. I want to modify the data it contains, but using numpy.array(myObject) seems to copy the data and thus my object is not modified. Am I mistaken or did I make a mistake in my array interface ?
I think numpy.array(object) always makes a copy.
You want numpy.asarray(object) which will make a view if object exposes the array interface and matches the type and sizes requested.
Thank you for the tip, I'll try it this evening. I checked that the data is actually copied, so this should be what I need ;) Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher
I think numpy.array(object) always makes a copy.
You want numpy.asarray(object) which will make a view if object exposes the array interface and matches the type and sizes requested.
FYI, numpy.asarray is a shortcut for numpy.array(copy=False), numpy.asanyarray for numpy.array(copy=False, subok=True)... In other terms, you can stick to array, as long as you provide the proper keywords.
participants (3)
-
Christopher Barker -
Matthieu Brucher -
Pierre GM