【问题标题】:TypeError: unsupported operand type(s) for |: 'float' and 'bool' for if else conditionTypeError: 不支持的操作数类型 |: 'float' 和 'bool' 用于 if else 条件
【发布时间】:2020-07-02 23:32:07
【问题描述】:

我知道这个话题已经讨论了很多,比如TypeError: unsupported operand type(s) for |: 'str' and 'bool', 但我发现没有人能解决我的问题。 我有一个庞大的数据框: 一栏是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

另一列是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

不为零的行很容易选择,但是

#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'

我真的不明白,因为我只是使用Stackoverflow中讨论的建议,我认为其他人可能有同样的问题,所以我在这里发布它

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      在 Python 中|bitwise 布尔或运算符,看来您实际上想要一个逻辑或运算符:or

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

      代替:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-22
        • 2021-03-03
        • 2017-06-06
        • 2018-10-30
        • 2014-03-28
        • 2018-06-20
        • 2017-08-23
        • 1970-01-01
        相关资源
        最近更新 更多