TypeError:不支持的操作数类型 - :' str'和' numpy.ndarray'

时间:2015-09-15 15:31:32

标签: python numpy

我遇到了min()max()和str以及numpy.ndarray的问题。我收到这个错误:

TypeError: unsupported operand type(s) for -: 'str' and 'numpy.ndarray'

我怀疑问题出在定义haHigh0的行中的haUpLeg0,我将非常感谢我如何纠正。我已经通过堆栈查看了一些类似的问题,但似乎无法修复以转换为我的问题。

数据:

Date        Time    Open    High    Low    Close    Volume  Ticker 
02/01/2015  14:30   79.52   79.73   79.52   79.71   10841   DVY 
02/01/2015  14:31   79.6999 79.6999 79.61   79.67   1426    DVY 
02/01/2015  14:32   79.69   79.7    79.69   79.7    800     DVY 
02/01/2015  14:33   79.7    79.759  79.7    79.759  1158    DVY 
02/01/2015  14:34   79.747  79.747  79.73   79.73   1125    DVY 
02/01/2015  14:35   79.73   79.73   79.71   79.71   301     DVY 
02/01/2015  14:36   79.8    79.82   79.75   79.82   7818    DVY 
02/01/2015  14:37   79.82   79.83   79.778  79.82   6985    DVY

代码:

mkt_data = index_data[['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']]
open0 = mkt_data['Open'] #
high0 = mkt_data['High'] #
low0 = mkt_data['Low'] #
close0 = mkt_data['Close'] #
date0 = mkt_data['Date'] #
time0 = mkt_data['Time'] #
ticker0 = index_data['Ticker'] #

close1 = mkt_data['Close'].shift(1) #
open1 = mkt_data['Open'].shift(1) # 
high1 = mkt_data['High'].shift(1) #
low1 = mkt_data['Low'].shift(1) #
high2 = mkt_data['High'].shift(2) #
low2 = mkt_data['Low'].shift(2) #
close2 = mkt_data['Close'].shift(2) # 
open2 = mkt_data['Open'].shift(2) #
close3 = mkt_data['Close'].shift(3) # 
open3 = mkt_data['Open'].shift(3) #

haClose2 = (open2 + high2 + low2 + close2) / 4  #
haOpen2 = (close3 + open3) / 2  #       

haClose1 = (open1 + high1 + low1 + close1) / 4  #
haOpen1 = (haClose2 + haOpen2) / 2  #
haHigh1 = max('high1', 'haOpen1', 'haClose1') #
haLow1 = min('low1', 'haOpen0', 'haClose1') # 

haClose0 = (open0 + high0 + low0 + close0) / 4 #
haOpen0 = (haClose1 + haOpen1) / 2 #


haHigh0 = max('high0', 'haOpen0', 'haClose0') #
haLow0 = min('low0', 'haOpen0', 'haClose0')  #
haUpLeg0 = (haHigh0 - haClose0) / (close1) #

2 个答案:

答案 0 :(得分:2)

在底部附近,您似乎可以互换地使用变量名称和字符串,例如:

haClose1 = (open1 + high1 + low1 + close1) / 4  #
haOpen1 = (haClose2 + haOpen2) / 2  #
haHigh1 = max('high1', 'haOpen1', 'haClose1') #
haLow1 = min('low1', 'haOpen0', 'haClose1') # 

您可能希望继续使用变量:

haClose1 = (open1 + high1 + low1 + close1) / 4  #
haOpen1 = (haClose2 + haOpen2) / 2  #
haHigh1 = max(high1, haOpen1, haClose1) #
haLow1 = min(low1, haOpen0, haClose1) # 

也就是说,由于-(以及+等)strnumpy.ndarray之间的操作数类型不受支持,因此您收到TypeError - 你不能从另一个中减去一个(或加一个)。

答案 1 :(得分:2)

如果你想找到三个中的最高值 - high0,haOpen0,haClose0 - 你可以做到

max(max(high0),max(haOpen0),max(haClose0))

我认为你不应该在变量名称周围使用引号。 ' high0'是一个字符串,但是high0是一个熊猫系列。