Michael
Michael

Reputation: 559

TypeError: unsupported operand type(s) for |: 'float' and 'bool' for if else condition

I know this topic has been discussed a lot like TypeError: unsupported operand type(s) for |: 'str' and 'bool', but I found no one can solve my question. I have a massive dataframe: one column is df['Closed P/L']

df['Closed P/L'].loc[df['Closed P/L']!=0]
21        9.20
22      559.70
23     -455.30
24      481.67
25    -1825.50
27      -98.92
28     -473.94
29        2.80
31       21.20
33       28.00
34     -172.00
35      -12.87
36      137.02
37       11.04
39      739.23
40      323.59

another column is df['Floating P/L']:

df['Floating P/L'].loc[df['Floating P/L']!=0]
39      -340.97
42      -844.20
43     -2383.84
44     -2415.48
45      -172.00
47     -1706.04
83      -259.61
91     -7544.43

The rows which are not zero can be chosen easily, but

#exclude the day when closed pnl and floating pnl are zero
if (df['Closed P/L'].loc[df['Closed P/L']!=0]) | (df['Floating P/L'].loc[df['Floating P/L']!=0]):
    df['Returns'] = df['New_Balance']/df['New_Balance'].shift(1) -1
else:
    df['Returns'] = 0

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1788         try:
-> 1789             result = op(x, y)
   1790         except TypeError:

TypeError: unsupported operand type(s) for |: 'float' and 'bool'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-27-7c03e0bf86bc> in <module>
      1 #exclude the day when closed pnl and floating pnl are both zero
----> 2 if (df['Closed P/L'].loc[df['Closed P/L']!=0]) | (df['Floating P/L'].loc[df['Floating P/L']!=0]):
      3     df['Returns'] = df['New_Balance']/df['New_Balance'].shift(1) -1
      4 else:
      5     df['Returns'] = 0

~\anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other)
   1848         filler = (fill_int if is_self_int_dtype and is_other_int_dtype
   1849                   else fill_bool)
-> 1850         res_values = na_op(self.values, ovalues)
   1851         unfilled = self._constructor(res_values,
   1852                                      index=self.index, name=res_name)

~\anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1795                 x = ensure_object(x)
   1796                 y = ensure_object(y)
-> 1797                 result = libops.vec_binop(x, y, op)
   1798             else:
   1799                 # let null fall thru

pandas\_libs\ops.pyx in pandas._libs.ops.vec_binop()

pandas\_libs\ops.pyx in pandas._libs.ops.vec_binop()

TypeError: unsupported operand type(s) for |: 'float' and 'bool'

I really dont get it, as I just use the suggestion discussed in Stackoverflow, I assume others may have the same problem, so I post it here

Upvotes: 0

Views: 11485

Answers (2)

Jack Aidley
Jack Aidley

Reputation: 20107

In Python | is the bitwise boolean or operator, it seems that you actually want a logical or operator: or.

if 3.4 or False :
  print("works!")

instead of:

if 3.4 | False :
  print("Type error!")

Upvotes: 0

Pramote Kuacharoen
Pramote Kuacharoen

Reputation: 1541

df['Returns'] = 0
df.loc[(df['Closed P/L'] != 0) & (df['Floating P/L'] != 0), 'Returns']
    = df['New_Balance']/df['New_Balance'].shift(1) - 1

Upvotes: 2

Related Questions