I am importing a csv file
data = np.genfromtxt('na.csv', delimiter=",", dtype=[('latitude', 'f8'), ('longitude', 'f8'), ('location_id','i4'), ('location_name', 'S60'), ('location_group_id', 'i4'), ('location_group_name', 'S32')])
and considering rows by location_group_ids, one by one.
l_g_id_set = set()
l_g_id_set.update(data['location_group_id'])
for lgid in l_g_id_set:
# rows with location group id == lgid
group = data[data['location_group_id']==lgid]
So far, I only included latitude and longitude, which are two float values in the 0th and 1st position of the structured array from the csv file.
# structured array of latitude-longitude
latlon = group[list(group.dtype.names[:2])]
# convert the structured array into numpy array of floats
llarray = latlon.view((float, len(latlon.dtype.names)))
Now I want to include location_id, which is an integer value in the 2nd position of the array, to latlon and llarray. Rather than making this another structured array, I'd want llarray a 2D float array with 3 columns for ease of calculation.
However when I try the following, only changing 2 to 3
# structured array of latitude-longitude
latlon = group[list(group.dtype.names[:3])]
# convert the structured array into numpy array of floats
llarray = latlon.view((float, len(latlon.dtype.names)))
it fails, throwing the following error.
llarray = latlon.view((float, len(latlon.dtype.names)))
ValueError: new type not compatible with array.
How can I fix this, and why is my fix failing?
np.ones((3,),dtype=int).view(float)produces the same error.