Carbon
Carbon

Reputation: 3973

Why can't I associate a datatype with two floats and a string with an array with two floats and a string?

I believe the below code should work, but indeed it does not, claiming ValueError: could not convert string to float: 'hi' - why is it trying to convert the string to a float?

import numpy as np
z = {}
dt = np.dtype([('num1',np.float_),('num2',np.float_),('nm',np.unicode_,8)])
z['one'] = np.array([1.0,2.0,'hi'],dt);
z['two'] = np.array([4.0,5.0,'mom'],dt);

Upvotes: 1

Views: 21

Answers (1)

mrzo
mrzo

Reputation: 2145

It works for me if I group the elements by adding parentheses:

import numpy as np

z = {}
dt = np.dtype([('num1',np.float_),('num2',np.float_),('nm',np.unicode_,8)])
z['one'] = np.array([(1.0,2.0,'hi')], dt);
z['two'] = np.array([(4.0,5.0,'mom')], dt);

Upvotes: 1

Related Questions