python - ValueError: Error parsing datetime string NumPy -
i trying convert string date date format in numpy array. using datetime64 datatype cast off seconds , receive error. code below. write numpy datatype date type database.
import json import jsonpickle import requests import arcpy import numpy np #note import random import timestring fc = "c:\mylatesting.gdb\myla311copy" if arcpy.exists(fc): arcpy.delete_management(fc) f2 = open('c:\users\administrator\desktop\detailview.json', 'r') data2 = jsonpickle.encode( jsonpickle.decode(f2.read()) ) url2 = "myurl" headers2 = {'content-type': 'text/plain', 'accept': '/'} r2 = requests.post(url2, data=data2, headers=headers2) decoded2 = json.loads(r2.text) dt = np.dtype([('sraddress', 'u40'), ('latitudeshape', '<f8'), ('longitudeshape', '<f8'), ('latitude', '<f8'), ('longitude', '<f8'), ('type', 'u40'), ('srnumber', 'u40'), ('firstname', 'u40'), ('lastname', 'u40'), ('homephone', 'u40'), ('createdate', 'datetime64[s]'), ('comment', 'u128'), ('iteminfo', 'u128'), ('daytest', 'u128'), ('districtname', 'u128'), ('shortday', 'u128'), ('parentnumber', 'u128'), ('a_call_no','u128'), ('area', 'u128'), ('directionsuffix','u128'), ('districtabbr', 'u128'), ('districtnumber', 'u128'), ('districtoffice', 'u128'), ('fraction', 'u128'), ('r_call_no', 'u128'), ('sectionid', 'u128'), ('streetto', 'u128'), ('streetfrom', 'u128'), ('streetlightid', 'u128'), ('streetlightstatus', 'u128'), ('y_call_no', 'u128'), ('communityplanningarea', 'u128'), ('lastupdatedby', 'u128'), ('bosradioholdername', 'u128'), ]) items = [] sr in decoded2['response']['listofservicerequest']['servicerequest']: sraddress = sr['sraddress'] latitude = sr['latitude'] longitude = sr['longitude'] srnumber = sr['srnumber'] firstname = sr['firstname'] lastname = sr['lastname'] homephone = sr['homephone'] createddate = sr['createddate'] print createddate iteminfo = " " ew in sr["listofla311electronicwaste"][u"la311electronicwaste"]: commoditytype = ew['type'] itemtype = ew['electronicwesttype'] itemcount = ew['itemcount'] iteminfo += '{0}, {1}, '.format(itemtype, itemcount) parentnumber = ew['name'] gis in sr["listofla311gislayer"][u"la311gislayer"]: day = gis['day'] districtname = gis['districtname'] shortday = gis['shortday'] a_call_no = gis['a_call_no'] area = gis['area'] directionsuffix = gis['directionsuffix'] districtabbr = gis['districtabbr'] districtnumber = gis['districtnumber'] districtoffice = gis['districtoffice'] fraction = gis['fraction'] r_call_no = gis['r_call_no'] sectionid = gis['sectionid'] streetfrom = gis ['streetfrom'] streetto = gis ['streetto'] streetlightid = gis ['streetlightid'] streetlightstatus = gis['streetlightstatus'] y_call_no = gis ['y_call_no'] communityplanningarea = gis['communityplanningarea'] lastupdatedby = gis['lastupdatedby'] bosradioholdername = gis['bosradioholdername'] comments = [ cl['comment'] cl in sr["listofla311servicerequestnotes"][u"la311servicerequestnotes"]] print comments comment = ' '.join(comments) items.append((sraddress, latitude, longitude, latitude, longitude, commoditytype, srnumber, firstname, lastname, homephone, createddate, comment, iteminfo, day, districtname, shortday, parentnumber, a_call_no, area, directionsuffix, districtabbr, districtnumber, districtoffice, fraction, r_call_no, sectionid, streetfrom, streetto, streetlightid, streetlightstatus, y_call_no, communityplanningarea, lastupdatedby, bosradioholdername )) arr = np.array(items,dtype=dt) sr = arcpy.spatialreference(4326) arcpy.da.numpyarraytofeatureclass(arr, fc, ['longitudeshape', 'latitudeshape'], sr ) print json.dumps(decoded2, sort_keys=true, indent=4) file "c:/users/administrator/desktop/devsummitjson_pyseminar.py", line 166, in <module> arr = np.array(items,dtype=dt) valueerror: error parsing datetime string "02/17/2015 16:53:25" @ position 2
just looking @ error message get, you've input datetime in wrong string format
numpy.datetime64("02/17/2015 16:53:25") traceback (most recent call last): file "<interactive input>", line 1, in <module> valueerror: error parsing datetime string "02/17/2015 16:53:25" @ position 2 numpy.datetime64("2015-02-17t16:53:25") >>> numpy.datetime64('2015-02-17t16:53:25+0100')
beware numpy assumes time given in local timezone (here utc+1). append "z" , interpreted utc.
so have either change format of string, or try solution using pandas presented here seems more flexible in interpreting string formats
Comments
Post a Comment