0

I simply want it so when the temperature is above 15.5 AND there is hail I want hail to equal 3 and otherwise I just want hail to equal hail.

hail = np.where((hail > 0 and tmp > 15.5).all, 3, hail)

My error is:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Looks like there are two problems in the above code: 1) `and` means it's converting the entire series to a boolean, then doing the and. You probably want `&`, which does elementwise and. 2) Using `(...).all()` means that you're turning the entire series into a single boolean, which means that `np.where()` will do the same thing to every element. Probably not what you want. I suggest changing this to `hail = np.where((hail > 0 & tmp > 15.5), 3, hail)` – Nick ODell Jan 08 '22 at 21:02
  • 1
    @Nick, you need more (), `(hail > 0) & (tmp > 15.5)` – hpaulj Jan 08 '22 at 22:06
  • The duplicate has a lot of answers, so it can be hard to identify which applies to your case. This ambiguity error arises when ever you try to use arrays in a context that expects a scalar True/False value. `and` is one such case. Due to operator precedence, you want to group the `>` tests to evaluate first, and then use the `&` (or `logical_and` to combine them, giving the `where` a boolean array as condition. – hpaulj Jan 08 '22 at 23:39

0 Answers0