Geoffrey West
Geoffrey West

Reputation: 281

ValueError: could not convert string to float, NumPy

I have a script where I am writing a JSON web-service to an Esri file geodatabase. I am receiving the error ValueError: could not convert string to float: Microwaves

I have used the exact same script before with U40 being the dtype for all strings.

My script and results are below;

import json
import jsonpickle
import requests
import arcpy
import numpy

fc = "C:\MYLATesting.gdb\MYLA311"
if arcpy.Exists(fc):
  arcpy.Delete_management(fc)

f = open('C:\Users\Administrator\Desktop\myla311.json', 'r')

data = jsonpickle.encode( jsonpickle.decode(f.read()) )

url = "myUrl"
headers = {'Content-type': 'text/plain', 'Accept': '/'}

r = requests.post(url, data=data, headers=headers)
sr = arcpy.SpatialReference(4326)

decoded = json.loads(r.text)

SRAddress = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['SRAddress']
latitude = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['Latitude']
longitude = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['Longitude']

CommodityType = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['Type']
ItemType = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['ElectronicWestType']
ItemCount = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['ItemCount']


CommodityType1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['Type']
ItemType1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['ElectronicWestType']
ItemCount1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['ItemCount']






print SRAddress
print latitude
print longitude

print CommodityType
print ItemType
print ItemCount






print CommodityType1
print ItemType1
print ItemCount1



item ={'SRAddress': SRAddress, 'Longitude': longitude, 'Latitude': latitude, 'CommodityType': CommodityType, 'ItemType': ItemType, 'ItemCount': ItemCount}
import numpy as np     #NOTE THIS
keys = ['SRAddress','Longitude','Latitude','CommodityType','ItemType', 'ItemCount']
k1,k2,k3, k4, k5, k6 = keys
data_line ={'SRAddress': SRAddress, 'Longitude': longitude, 'Latitude': latitude, 'CommodityType': CommodityType, 'ItemType': ItemType, 'ItemCount': ItemCount}
frmt = '\nStraight dictionary output\n Address: {} Long: {} Lat: {}'
print(frmt.format(item[k1],item[k2],item[k3], item[k4],item[k5], item[k6]))
print '\noption 1:  List comprehension with unicode'
a =  tuple([unicode(item[key]) for key in keys])  # list comprehension with unicode
print('{}'.format(a))
dt = np.dtype([('SRAddress','U40'),('CommodityType','U40'), ('ItemType','U40'), ('ItemCount','U40'),('longitude','<f8'),('latitude','<f8')])
arr = np.array(a,dtype=dt)
print'\narray unicode\n',arr
print'dtype',arr.dtype
print '\noption 2:List comprehension without unicode'
b = tuple([item[key] for key in keys])
print('{}'.format(b))
dt = np.dtype([('SRAddress','U40'),('CommodityType','U40'), ('ItemType','U40'), ('ItemCount','U40'),('longitude','<f8'),('latitude','<f8')])
arr = np.array(b,dtype=dt)
print'\narray without unicode\n',arr
print'dtype',arr.dtype

arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitude', 'latitude'], sr)

Results

C:\Python27\ArcGIS10.2\python.exe C:/MYLAScripts/MYLAJson.py
Traceback (most recent call last):
5810 N WILLIS AVE, 91411
  File "C:/MYLAScripts/MYLAJson.py", line 71, in <module>
34.176277
    arr = np.array(a,dtype=dt)
-118.455249
ValueError: could not convert string to float: Microwaves
Electronic Waste
Microwaves
3
Electronic Waste
Televisions (Any Size)
6

Straight dictionary output
 Address: 5810 N WILLIS AVE, 91411 Long: -118.455249 Lat: 34.176277

option 1:  List comprehension with unicode
(u'5810 N WILLIS AVE, 91411', u'-118.455249', u'34.176277', u'Electronic Waste', u'Microwaves', u'3')

Upvotes: 1

Views: 3044

Answers (1)

Iguananaut
Iguananaut

Reputation: 23366

You have

keys = ['SRAddress','Longitude','Latitude','CommodityType','ItemType', 'ItemCount']

then the script is making a tuple of values from the dict items using these keys in that order:

a =  tuple([unicode(item[key]) for key in keys])

Then when you convert this tuple to an array

arr = np.array(a,dtype=dt)

it is trying to stuff the value associated with ItemType into the longitude field of the struct. The keys list should be in the same order as the struct fields of your dtype. Ideally you wouldn't even bother copying this information and instead use dt.names. Then as long as the struct fields have the same names as the dict you're trying to convert it should take values in the correct order.

Upvotes: 1

Related Questions