[Numpy-discussion] replacing Nan's in a string array converted from a float array
Thu May 7 19:00:43 CDT 2009
On Thu, May 7, 2009 at 19:36, Brennan Williams
> I've created an array of strings using something like....
> If the array value is a Nan I get "1.#QNAN" in my string array.
> For cosmetic reasons I'd like to change this to something else, e.g.
> "invalid" or "inactive".
> My string array can be up to 100,000+ values.
> Is there a fast way to do this?
Well, there is a print option that lets you change how nans are
represented when arrays are printed. It is possible that this setting
should also be used when converting to string arrays. However, it does
not do so currently:
In : %push_print --nanstr invalid
Edge items: 3
Line width: 75
In : a = zeros(10)
In : a = nan
In : a
array([ 0., 0., 0., 0., 0., invalid, 0.,
0., 0., 0.])
In : a.astype('|S8')
array(['0.0', '0.0', '0.0', '0.0', '0.0', 'nan', '0.0', '0.0', '0.0', '0.0'],
You will need to use the typical approach:
mask = (stringarray == '1.#QNAN')
stringarray[mask] = 'invalid'
This will be wasteful of memory, so with your large array size, you
might want to consider breaking it into chunks and modifying the
chunks in this way.
"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