0

I have this line of code:

df['volume_up'] = np.where(df['trade_direction'] == True, df['volume'], float('nan'))

and I get this warning:

enter image description here

but if I change the statement to

df['trade_direction'] is True

it will not work anymore.

Why am I getting this warning? or am I implementing the change the wrong way?

Thomas
  • 10,933
  • 14
  • 65
  • 136
  • I'd ignore it; your PEP8 analyzer just doesn't know numpy. – Aaron D. Marasco Aug 20 '20 at 22:32
  • Just ignore it. `numpy` breaks the assumption that `==` returns a boolean object, thus that part of the PEP doesn't apply. Unfortunately I don't think there is a way to write a PEP8 analyzer that [prevents foolish consistency](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds). – juanpa.arrivillaga Aug 20 '20 at 22:35
  • You can use the second suggestion: `np.where(df['trade_direction'], df['volume'], float('nan'))` and it'll produce your expected results and quiet the linter – Paul H Aug 20 '20 at 22:41
  • how would I go about handling the opposite (==False) case in a Numpy and PEP friendly way? (I have some lines with True, some with False) – Thomas Aug 20 '20 at 22:55
  • You can use `~df['trade_direction']` – juanpa.arrivillaga Aug 20 '20 at 23:37

0 Answers0