Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe' #11549

Closed
sdsunjay opened this issue Jul 11, 2018 · 2 comments

Comments

@sdsunjay
Copy link

Using OS X 10.12 and using Version: 1.15.0rc2 of numpy. I'm seeing this error:

  File "temp.py", line 103, in computeMACD
    emaslow = ExpMovingAverage(x, slow)
  File "temp.py", line 93, in ExpMovingAverage
    a =  np.convolve(values, weights, mode='full')[:len(values)]
  File "/Users/x/xx/graph/lib/python3.6/site-packages/numpy/core/numeric.py", line 1045, in convolve
    return multiarray.correlate(a, v[::-1], mode)
TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

I'm seeing this error when using this widely available code:

 def ExpMovingAverage(values, window):
     weights = np.exp(np.linspace(-1., 0., window))
     weights /= weights.sum()
     a =  np.convolve(values, weights, mode='full')[:len(values)]
     a[:window] = a[window]
     return a

My values are real stock prices, ['93.89', '89.89', '87.17', '90.57', '88.92', '90.46'...]. Window is 26. I'm not sure why so many people reference the above code that is not working for me. Any ideas what is wrong here?

@bashtage
Copy link
Contributor

bashtage commented Jul 11, 2018

Your data are strings. They should be numbers.

 ['93.89', '89.89', '87.17', '90.57', '88.92', '90.46'...]

should be

 [93.89, 89.89, 87.17, 90.57, 88.92, 90.46]

This is verified using

v = ['93.89', '89.89', '87.17', '90.57', '88.92', '90.46']*30
ExpMovingAverage(v,10)

TypeError: Cannot cast array data from dtype('float64') to 
dtype('<U32') according to the rule 'safe'

and

ExpMovingAverage(list(map(float,v)),10)

array([89.6938941 , 89.6938941 , 89.6938941 , 89.6938941 ...

@mattip mattip closed this as completed Jul 11, 2018
@sdsunjay
Copy link
Author

Thank you. This fixed my issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants