0

So I have these 4 lines of code, I've been trying for hours to figure out how to get this to work. A caveat is that I cannot use a for loop to fix this:

if(np.logical_and((signals['signal'] == 0),(signals['rsip']<=20))):
        signals['signal'] = 1.0
if((signals['signal'] == 1.0) & (signals['rsip']>=80)):
        signals['signal'] = 0.0

I keep running into the error pointed at the first line:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

And I'm not sure what's going wrong... As you can see I even tried using numpy logicals to no avail.

Mikey Mike
  • 79
  • 1
  • 9
  • Python `if` only takes a scalar True or False. `numpy` logicals working with an array or pandas series produce a multiple True/False elements (print the `&` expression for yourself). That's what the ambiguity is about. – hpaulj Aug 01 '18 at 05:08
  • @hpaulj, so I switched the notation to &, and still get the same error... Any ideas? – Mikey Mike Aug 01 '18 at 18:52
  • `(signals['signal'] == 1.0) & (signals['rsip']>=80)` is still a Series or array, multiple values. What exactly are you trying to do? The `if` expression lets you do a one-time choice. That's regular Python. An `if` cannot be used for individual elements of the array without a loop. – hpaulj Aug 01 '18 at 18:58
  • Oh I think I'm starting to understand... that's why pd.where() is probably preferential for sorting through the series one by one. This question was actually an attempt to conceptualize an earlier question of mine outlined here: https://stackoverflow.com/questions/51624113/np-where-and-ambiguous-boolean-causing-rsi-to-end-early – Mikey Mike Aug 01 '18 at 19:06
  • @hpaulj, thank you for the insight – Mikey Mike Aug 01 '18 at 19:13
  • @hpaulj, if you wouldn't mind taking a quick look at the question I linked, I'd greatly appreciate it. You seem to have a much better understanding about inherent data types and pandas than I do. – Mikey Mike Aug 01 '18 at 20:40

0 Answers0