0

I have an array with some zero values that i wish to convert in nan values. When i apply the code
all values become nan

myarray
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

myarray.shape
(64L, 52L)
myarray.max()
4563.666015625
myarray.min()
0.0

i wish to convert zero values in nan. I use an example from stackoverflow

a = np.arange(3.0)
a
array([ 0.,  1.,  2.])
a[a==0] = np.nan
a
array([ nan,   1.,   2.])

when i apply the example to my array all values became nan

myarray[myarray == 0.] = nan
myarray
array([[ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       ..., 
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan]])

myarray.max()
nan
myarray.min()
nan

1 Answer 1

5

It's not that all the values become nan, it's that (1) you're only looking at the parts that do, and (2) min and max don't work well with nans.

For example, if we make an array resembling yours:

>>> myarray = np.zeros((64, 52))
>>> myarray[3:-3,3:-3] = np.random.uniform(0, 5000, (64-6,52-6))
>>> myarray
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])
>>> myarray[myarray==0] = np.nan
>>> myarray
array([[ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       ..., 
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan]])

It may look like it's all nan, but it's not:

>>> myarray[2:5, 2:5]
array([[           nan,            nan,            nan],
       [           nan,  1500.05326562,  4583.70521213],
       [           nan,  4896.62420284,   892.83210033]])

You can also use nanmin and nanmax, which ignore nans:

>>> myarray.min()
nan
>>> myarray.max()
nan
>>> np.nanmin(myarray)
0.60474162939361253
>>> np.nanmax(myarray)
4996.8967777356092
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @DSM. is it possible with np.nan compute also the standard deviation and the mode?
There's nanstd; I don't think there's nanmode, though. You could always compute the mode of the non-nan values manually after removing them, though (myarray[~np.isnan(myarray)]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.