4

I have an array

a = np.array([1,2,3,4,np.nan])

I would like to replace anything which is less than 1.5 by np.nan, i.e. I would like

a = np.array([np.nan,2,3,4,np.nan])

how do I do that?

I did

 a[a<1.5] = np.nan

I got the following run time warning error in IPython (Py3.4) RuntimeWarning: invalid value encountered in less. Is this because my list had np.nan? Is there anything I can do to prevent this?

Also is there a way doing this inline without assigning? Instead of doing

a[a<1.5]=np.nan 
return a 

I can just do

 return a... 

where that .... is something that need filling in.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Lost1
  • 990
  • 1
  • 14
  • 34
  • @BradSolomon i meant, I am writing this in the middle of a program. is there an inplace replacement function? – Lost1 Sep 06 '17 at 21:02

2 Answers2

4

Is this [RuntimeWarning] because my list had np.nan?

Yes.

Is there anything I can do to prevent this?

In your case, this warning can be safely ignored. So that you don't accidentally suppress unrelated warnings, please don't put anything else inside the context manager besides the one line shown.

>>> import numpy as np
>>> a = np.array([1,2,3,4,np.nan])
>>> with np.errstate(invalid='ignore'):
...     a[a<1.5] = np.nan
...     
>>> a
array([ nan,   2.,   3.,   4.,  nan])

This operates in-place, a copy is not created here. To return a copy, with the original a unmodified, prefer the masked array approach.

wim
  • 338,267
  • 99
  • 616
  • 750
  • 2
    I don't think it's actually about the in-place operation (well, `a[a<1.5]` = np.nan is also in-place) but about avoiding assignment so that it can be done in one-line. – MSeifert Sep 06 '17 at 21:09
1

Another option that gets you to your return statement as desired:

mask = ~np.isnan(a)
mask[mask] &= a[mask] < 1.5
return np.where(mask, np.nan, a)

Example:

def ma_lessthan(arr, num):
    mask = ~np.isnan(arr)
    mask[mask] &= arr[mask] < num
    return np.where(mask, np.nan, arr)

print(ma_lessthan(a, 1.5))
[ nan   2.   3.   4.  nan]

mask credit to: @Jaime.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
  • 1
    It should be mentioned: this creates a copy. – wim Sep 06 '17 at 21:25
  • It is avoidable, and I showed [how](https://stackoverflow.com/a/46084308/674039). Using a masked array approach is good too, mind you, I just thought this was worth mentioning because we often care about details of performance and memory usage when using numpy. – wim Sep 06 '17 at 21:31