Karl Hunt
Karl Hunt

Reputation: 41

TypeError: return arrays must be of ArrayType

epsData is a two-dimensional array consisting of Dates and StockID.

I took out some of the code in order to make it simple.

The code calls the functions Generate and neweps, epsData is passed by the engine. I am not sure why it gives an error when I try to pass the array epsss to the SUE() function.

I tried to remove the extra bracket in array (if any) by using flatten function but that does not help.

SUE() is supposed to loop through the array and find the 4th last different value and then store these in an array.

I get this error:

TypeError: return arrays must be of ArrayType

with the three lines marked below:

def lastdifferentvalue(vals,datas,i):
  sizes=len(datas)
  j=sizes-1
  values=0
  while (i>0) and (j>=0):
    if logical_and((vals-datas[j]!=0),(datas[j]!=0),(datas[j-1]!=0)): # !! HERE !!
      i=i-1
      values=datas[j-1]
    j=j-1
  return j, values

def SUE(datas):
  sizes=len(datas)
  j=sizes-1
  values=0
  sues=zeros(8)
  eps1=datas[j]
  i=7
  while (j>0) and (i>=0) :
    counts, eps2=lastdifferentvalue(eps1,array(datas[0:j]),4)
    if eps2!=0:
      sues[i]=eps1-eps2
      i=i-1
      j,eps1=lastdifferentvalue(eps1,datas[0:j],1) # !! HERE !!

  stddev=std(SUE)
  sue7=SUE[7]
  return stddev,sue7          

def Generate(di,alpha):      

    #the code below loops through the data. neweps is a two dimensional array of floats [dates, stockid]                     
    for ii in range(0,len(alpha)):
      if (epss[2,ii]-epss[1,ii]!=0) and (epss[2,ii]!=0) and (epss[1,ii]!=0):
        predata=0
        epsss= neweps[di-delay-250:di-delay+1,ii]
        stddevs,suedata= SUE(array(epsss.flatten())) # !! HERE !!

Upvotes: 1

Views: 9142

Answers (2)

Ami Tavory
Ami Tavory

Reputation: 76381

Presumably, you're using numpy.logical_and, in the form of

np.logical_and(a, b, c)

with the meaning that you'd like to take the logical and of the three. If you check the documentation, though, that's not what it does. It's interpreting c as the array where you intend to store the results.

You probably mean here something like

np.logical_and(a, np.logical_and(b, c))

or

from functools import reduce
reduce(np.logical_and, [a, b, c])

Upvotes: 2

donkopotamus
donkopotamus

Reputation: 23236

The line:

if logical_and((vals-datas[j]!=0),(datas[j]!=0),(datas[j-1]!=0))

has two errors:

  1. Presumably you are wanting to perform a logical_and over (vals-datas[j] != 0) and (datas[j] != 0) and (datas[j-1] != 0). However numpy.logical_and only takes two input parameters, the third if passed is assumed to be an output array. Thus if you are wishing to have numpy.logical_and operate over three arrays it should be expressed as:

    logical_and(logical_and((vals-datas[j] != 0), (datas[j] != 0)), (datas[j-1] != 0))
    
  2. In any case, using a logical_and in an if statement makes no sense. It returns an array and an array does not have a truth value. That is, the result of a logical_and is an array of booleans, some of which are true and some false. Are you wishing to check if they are all true? Or if at least some are true?

    If the former, then you should test it as:

    if numpy.all(logical_and(...)):
       ...
    

    And if the latter then test it as:

    if numpy.any(logical_and(...)):
       ...
    

Upvotes: 0

Related Questions