[Numpy-discussion] Generically Creating Intermediate Data Compatible with Either ndarray or MasledArray Types

Robert Kern robert.kern@gmail....
Tue Mar 11 15:21:07 CDT 2008

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

More information about the Numpy-discussion mailing list