简体   繁体   English

TypeError: 输入类型不支持 ufunc 'bitwise_and'

[英]TypeError: ufunc 'bitwise_and' not supported for the input types

I'm trying to write some code that will allow me quickly determine whether a variable is higher, lower, or equal to another variable within a dataframe at each index point.我正在尝试编写一些代码,使我能够快速确定一个变量在每个索引点处是否高于、低于或等于 dataframe 中的另一个变量。

As shown here:如此处所示:

import numpy as np
import datetime
import pandas as pd
import operator

previous_cross = 'both'

ops = {
    '+' : operator.add,
    '-' : operator.sub,
    '*' : operator.mul,
    '/' : operator.truediv,  # use operator.div for Python 2
    '%' : operator.mod,
    '^' : operator.xor,
    '<' : operator.lt,
    '<=' : operator.le,
    '==' : operator.eq,
    '!=' : operator.ne,
    '>=' : operator.ge,
    '>' : operator.gt
} 

#//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

df = pd.read_csv(r'C:\EURUSD_mt5_ticks.csv', parse_dates=['Date'])
df.columns = ['Date', 'Ask_Price', 'Ask_Volume', 'Bid_Price', 'Bid_Volume', 'Spread']
df['Date'] = pd.to_datetime(df['Date'].astype('datetime64[ns]'), exact=True, cache=True, format='%Y-%m-%d  %H:%M:%S.%f')  
MA1 = df['Ask_Price'].rolling(4).mean()
MA2 = df['Ask_Price'].rolling(21).mean()
df['MA1'] = MA1
df['MA2'] = MA2

#//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

def tbar_number(n):
        return tbar_count - n

    
def vcross(variable1_column, direction, variable2_column, vdataframe, width):

    global previous_cross


    
    #number of bars previous from the current bar, 
    width_value = tbar_number(width)
    
    for i in range(width_value+1):

        width_value_count = i
  
        #data from the index of each bar within the 'width' bar range
        v1_width_data = df.loc[width_value_count, variable1_column] 
        v2_width_data = df.loc[width_value_count, variable2_column]
                                    
            #if variable 1 crosses above or is equal to(depending on chosen symbol) variable 2, return true
        if (direction == '>' or '>=') & (ops[direction](v1_width_data, v2_width_data)) & (previous_cross == 'short' or 'both'):
            if (direction == '>=') and (v1_width_data == v2_width_data):
                return True
            if direction == '>':
                previous_cross = 'long'  
                return True
        
             #if variable 1 crosses below or is equal to(depending on chosen symbol) variable 2, return true
        elif (direction == '<' or '<=') & (ops[direction](variable1, variable2)) & (previous_cross == 'long' or 'both'):
            if (direction == "<=") and (v1_width_data == v2_width_data): 
                return True
            if direction == '<':
                previous_cross = 'short'
                return True

             #if variable 1 is equal to(depending on chosen symbol) variable 2, return true
        elif (direction == '==') & (ops[direction](variable1, variable2)):
            return True
        
        else:
            return False
        
#/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for tbar_count in range(len(df)): 
    
  if vcross('MA1', '>', 'MA2', df, 1):
  print("hi")

Dataframe data Dataframe 数据


Date    Ask_Price   Ask_Volume  Bid_Price   Bid_Volume  Spread  MA1 MA2
0   2021-08-11 00:00:00.686 1.17199 0.75    1.17195 0.75    0.00004 NaN NaN
1   2021-08-11 00:00:00.989 1.17201 2.43    1.17195 3.37    0.00006 NaN NaN
2   2021-08-11 00:00:05.004 1.17198 0.75    1.17196 4.87    0.00002 NaN NaN
3   2021-08-11 00:00:05.203 1.17200 0.75    1.17197 0.10    0.00003 1.171995    NaN
4   2021-08-11 00:00:10.654 1.17201 0.75    1.17198 0.10    0.00003 1.172000    NaN

But I get this error但我得到这个错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-f2dbb6db13b8> in <module>
     80 for tbar_count in range(len(df)):
     81 
---> 82     if vcross('MA1', '>', 'MA2', df, 1):
     83         print("hi")
     84 

<ipython-input-7-f2dbb6db13b8> in vcross(variable1_column, direction, variable2_column, vdataframe, width)
     55 
     56             #if variable 1 crosses above or is equal to(depending on chosen symbol) variable 2, return true
---> 57         if (direction == '>' or '>=') & (ops[direction](v1_width_data, v2_width_data)) & (previous_cross == 'short' or 'both'):
     58             if (direction == '>=') and (v1_width_data == v2_width_data):
     59                 return True

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I haven't been able to find the solution through google.我无法通过谷歌找到解决方案。 Most of the time the issue seems to be related to not encapsulating the conditions when using & but I have encapsulated everything already.大多数时候,问题似乎与使用 & 时未封装条件有关,但我已经封装了所有内容。

Can anyone lend me a hand?谁能帮我一把?

Careful with the use of or in your tests.小心使用or在你的测试中。 For example:例如:

In [351]: direction='++'; direction=='>' or '>='
Out[351]: '>='
In [352]: direction='>'; direction=='>' or '>='
Out[352]: True

This does a direction=='>' ;这是一个direction=='>' ; if that is True, it returns True;如果是,则返回 True; if False, it returns the value after the or .如果为 False,则返回or之后的值。 It is not the same as它不一样

In [354]: direction='++';(direction=='>') or (direction=='>=')
Out[354]: False
In [355]: direction='>';(direction=='>') or (direction=='>=')
Out[355]: True
In [356]: direction='>=';(direction=='>') or (direction=='>=')
Out[356]: True

and equivalent test is等效测试是

direction in ['>','>=']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Numpy TypeError:输入类型不支持 ufunc &#39;bitwise_and&#39;, - Numpy TypeError: ufunc 'bitwise_and' not supported for the input types, 输入类型 Numpy 数组不支持“bitwise_and” - 'bitwise_and' not supported for the input types Numpy array Stackplot “TypeError:输入类型不支持 ufunc 'isfinite'” - Stackplot “TypeError: ufunc 'isfinite' not supported for the input types” statsmodels 引发 TypeError:优化输入中的输入类型不支持 ufunc 'isfinite' - statsmodels raises TypeError: ufunc 'isfinite' not supported for the input types in Optimising Input 类型错误:输入类型不支持 ufunc &#39;isnan&#39;,-seaborn 热图 - TypeError: ufunc 'isnan' not supported for the input types, - seaborn Heatmap MatPlotLib、日期时间和类型错误:输入类型不支持 ufunc &#39;isfinite&#39;... - MatPlotLib, datetimes, and TypeError: ufunc 'isfinite' not supported for the input types… TypeError: ufunc 'isfinite' 不支持输入类型,并且无法安全地将输入强制转换为任何受支持的类型 - TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types AttributeError:Float' object 没有属性日志 /TypeError: 输入类型不支持 ufunc 'log' - AttributeError:Float' object has no attribute log /TypeError: ufunc 'log' not supported for the input types datetime64 值,输入类型不支持 ufunc &#39;isfinite&#39; - datetime64 values, ufunc 'isfinite' not supported for the input types openCV 中的屏蔽和 bitwise_and - Masking and bitwise_and in openCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM