0

If you compare np.nan to some other value and you do it in a np.frompyfunc, will raise a warning. Why is this? Is it a bug or a feature?

import numpy as np
func = np.frompyfunc(lambda x: x<0,nin=1,nout=1)
print(func(1))            # False, no warning
print(np.nan<0)           # False
print(func(np.nan))       # False, and raises a warning
Background and Motivation

I ran into this while debugging Why does numpy.vectorize give a warning about an invalid value when using uncertainties? The root cause was hidden behind both np.vectorize and some other library code. So understanding that float comparison changes semantics in a frompyfunc was a bit hard. It comes across as a bug to me.

In my own code, I like to use np.seterr(all='raise') and with np.errstate(some_group='ignore'): ... to make sure all warnings about float errors are handeled correctly and warnings are ignored only where I know it is safe. Therefore, I would like to know why this compare-with-nan is only occasionally warning.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
LudvigH
  • 3,662
  • 5
  • 31
  • 49
  • What warning do you get? I see no warnings with the above code. – chepner Mar 07 '23 at 18:38
  • @chepner, with 'raise', the error is `FloatingPointError: invalid value encountered in (vectorized)` when `np.nan` is one of the values. (or a `RuntimeWarning`). That's true even if the `lambda` itself produces a valid `bool`. – hpaulj Mar 07 '23 at 19:49
  • I'm closing this since it is answered in your other question. – hpaulj Mar 07 '23 at 19:55
  • @hpaulj Even with `np.seterr(all='raise')`, I don't see any warning from either of the calls to `func` shown. – chepner Mar 07 '23 at 20:22
  • @chepner, could it be a version issue? I see it with `1.23`. The duplicate talks about an issue with `np.frompyfunc` passing a `c` warning through. – hpaulj Mar 07 '23 at 20:39
  • @hpaulj Possibly; I'm testing with 1.24.2. (Not worth me pressing the point if others can, in fact, reproduce the issue.) – chepner Mar 07 '23 at 20:45
  • For future reference: the answer to this question is a comment to my answer in the other question – LudvigH Mar 07 '23 at 22:25

0 Answers0