Generically Creating Intermediate Data Compatible with Either ndarray or MasledArray Types
![](https://secure.gravatar.com/avatar/460d81097bac52e582b0ca5e9aa7525e.jpg?s=120&d=mm&r=g)
I have a function that I would like to work with both MaskedArray's and ndarray's. The only blocker for this particular function is the need to create some stand-in data that is appropriately either a MaskedArray or an ndarray. Currently I have: dummy = numpy.ones(data.shape, dtype=bool) where data has a dtype of float. I've already discovered that numpy.ones_like "does the right thing", but how do I do the equivalent in conjunction with declaring a new dtype? Said another way, how can a create arrays of the same class and (possibly) shape as an existing array, but with a different dtype? Thanks, Alex
![](https://secure.gravatar.com/avatar/7e9e53dbe9781722d56e308c32387078.jpg?s=120&d=mm&r=g)
Alex, I don't know if it works for older versions of numpy, but with svn you can simply use the astype() method of the array. If the array is masked it seems to work correctly, although it does not update the fill_value to the default for the new type. Eric Alexander Michael wrote:
I have a function that I would like to work with both MaskedArray's and ndarray's. The only blocker for this particular function is the need to create some stand-in data that is appropriately either a MaskedArray or an ndarray. Currently I have:
dummy = numpy.ones(data.shape, dtype=bool)
where data has a dtype of float. I've already discovered that numpy.ones_like "does the right thing", but how do I do the equivalent in conjunction with declaring a new dtype?
Said another way, how can a create arrays of the same class and (possibly) shape as an existing array, but with a different dtype?
Thanks, Alex _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/460d81097bac52e582b0ca5e9aa7525e.jpg?s=120&d=mm&r=g)
On Tue, Mar 11, 2008 at 3:42 PM, Eric Firing <efiring@hawaii.edu> wrote:
I don't know if it works for older versions of numpy, but with svn you can simply use the astype() method of the array. If the array is masked it seems to work correctly, although it does not update the fill_value to the default for the new type.
That will do even though I don't want to actually copy the data, as I want an array to hold intermediate data of the same shape. Incidentally, while ones_like appears to play nice with derived classes, empty_like and zeros_like do not seem to do the same.
![](https://secure.gravatar.com/avatar/d5aa64affb55c3d87e1f4d675ee483f0.jpg?s=120&d=mm&r=g)
Am Dienstag, 11. März 2008 20:57:30 schrieb Alexander Michael:
Incidentally, while ones_like appears to play nice with derived classes, empty_like and zeros_like do not seem to do the same.
Shouldn't this be fixed? (Obviously, this stems from the fact that ones_like is implemented in C, while the two others are helpers by fperez copied over from IPython.) Maybe using Robert's suggestion? (Patch attached.) Or implement them all the same? -- Ciao, / / /--/ / / ANS
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Tue, Mar 11, 2008 at 2:10 PM, Alexander Michael <lxander.m@gmail.com> wrote:
I have a function that I would like to work with both MaskedArray's and ndarray's. The only blocker for this particular function is the need to create some stand-in data that is appropriately either a MaskedArray or an ndarray. Currently I have:
dummy = numpy.ones(data.shape, dtype=bool)
where data has a dtype of float. I've already discovered that numpy.ones_like "does the right thing", but how do I do the equivalent in conjunction with declaring a new dtype?
Said another way, how can a create arrays of the same class and (possibly) shape as an existing array, but with a different dtype?
dummy = numpy.ones(data.shape, dtype=bool).view(type(data)) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (4)
-
Alexander Michael
-
Eric Firing
-
Hans Meine
-
Robert Kern