numexpr icon indicating copy to clipboard operation
numexpr copied to clipboard

How to make nan in an expression?

Open louisabraham opened this issue 7 years ago • 5 comments

I would like to produce a 'nan' expression without any warning. 'log(-1)' works for example but produces a warning.

see https://github.com/sympy/sympy/pull/12984#discussion_r127817271

louisabraham avatar Mar 28 '18 15:03 louisabraham

As per the warnings docs,

https://docs.python.org/3/library/warnings.html#temporarily-suppressing-warnings

import numpy as np
import numexpr as ne
import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    foo = ne.evaluate('log(-1)')
print(foo)

robbmcleod avatar Mar 28 '18 17:03 robbmcleod

Of course, but sometimes you want to see the relevant warnings. If you have a log error in the same expression, you will miss it and believe you got NaN for a legitimate reason.

louisabraham avatar Mar 28 '18 17:03 louisabraham

Warnings are not exceptions. This context manager will not suppress exceptions, only a try/except block would do that.

robbmcleod avatar Mar 28 '18 17:03 robbmcleod

I think I wasn't very clear and don't understand your point.

Let me show an example:

ne.evaluate('where(log(a)<2, 1, log(-1))')

I would like a = 1000 not to show a warning, but a = -1 should show a warning. Do you think it is possible? Currently the best solution would be to find a free name and to give it the value float('nan').

louisabraham avatar Mar 28 '18 17:03 louisabraham

Edit: The warning will only ever pop once. So if you suppress it, it doesn't show up again.

For your code fragment, probably not in 2.6. Can you inject some variable 'NaN' variable into the locals dict of the calling frame? You would have the same problem with pi for example.

The 3.0 branch (https://github.com/pydata/numexpr/tree/numexpr-3.0) can do that already:

import numexpr3 as ne3
import numpy as np
a = 1000
ne3.evaluate('where(log(a)<2, 1, np.nan')

I'm hoping to find some time this long weekend to work on the benchmarking once more and then it should be ready for a beta. Integration into down-stream dependencies would likely take quite some time, because some of them are so enormous (like pandas).

robbmcleod avatar Mar 28 '18 17:03 robbmcleod

Message to comment on stale issues. If none provided, will not mark issues stale

github-actions[bot] avatar Feb 19 '24 01:02 github-actions[bot]