1

I have written a script to add, among other stuff, a datetime column. I have used the arcpy.AddField_managementcommand, but it didn't have a 'Datetime' type, so I used the 'Date' type : arcpy.AddField_management(outZStat,"Datetime", "DATE") Bellow is a part of the script:

mth = raster[21:23]
dec = raster[30]
tm = 14
s2 =  str(day)
s3 = "/"
s4 = str(mth)
s5 = "/"
s6 = str(yr)
s7 = str(" 14:00")
str_dt = s2 +s3 +s4 +s5 +s6+s7
datet = datetime.strptime(str_dt, "%d/%m/%Y %H:%M")
print datet

I added the print just to be sure about the values of datet, its gives me exactly what I want: enter image description here

However, in the attribute table, the time is 00:00:00 for all the attributes in the Datetime field: enter image description here

Screen of the 1st row of my table: enter image description here

The problem may be coming from the type on the arcpy.AddField_managementbut I don't know how to 'force' the Datetime.

The script:

... arcpy.AddField_management(outZStat,"Datetime", "DATE") ... arcpy.AddField_management(outZStat,"Year", "SHORT") ... arcpy.AddField_management(outZStat,"Month", "SHORT") ... arcpy.AddField_management(outZStat,"Decade", "SHORT") ... arcpy.AddField_management(outZStat,"Time", "SHORT") ...
...
... mylist = [] ... with arcpy.da.SearchCursor(outZStat, '*') as arows: ... idx = arows.fields.index("OID") ... for arow in arows: ... mylist.append(arow[idx]) ... mylist_length = len(mylist) ... with arcpy.da.UpdateCursor(outZStat, "*") as cursor: ... i = 0 ... for row in cursor: ... if i< mylist_length: ... if raster[6] == "A": ... if raster[30]=="1": ... day = 01 ... elif raster[30]=="2": ... day = 11 ... else: ... day = 21 ...
... yr = raster[16:20] ... mth = raster[21:23] ... dec = raster[30] ... tm = 14 ... s2 = str(day) ... s3 = "/" ... s4 = str(mth) ... s5 = "/" ... s6 = str(yr) ... s7 = str(" 14:00") ... str_dt = s2 +s3 +s4 +s5 +s6+s7 ... datet = datetime.strptime(str_dt, "%d/%m/%Y %H:%M") ... print datet ... else: ...
... if raster[31]=="1": ... day = 01 ... elif raster[31]=="2": ... day = 11 ... else: ... day = 21 ... yr = raster[17:21] ... mth = raster[22:24] ... dec = raster[31] ... tm = 10 ... s2 = str(day) ... s3 = "/" ... s4 = str(mth) ... s5 = "/" ... s6 = str(yr) ... s7 = str(" 10:00") ... str_dt = s2 +s3 +s4 +s5 +s6+s7 ... datet = datetime.strptime(str_dt, "%d/%m/%Y %H:%M") ... print datet ...
... id = cursor.fields.index("OID") ... row = [id, cursor[1],cursor[2],cursor[3],cursor[4],cursor[5],cursor[6],datet,yr,mth,dec,tm] ... cursor.updateRow(row) ... i = i +1

[EDIT:] What's different in my question, comparing to the question Storing datetime values in Shapefiles?, is that fields in the attribute tables in ArcGis, don't have a "Datetime" type (they have only Date type, in opposition to those in qgis). Ther isn't either the "to_datetime" function in the field calculator of ArcGis.

The easiest method to resolve my problem is to turn the field type of my attribute table to string. That's what I did finally.

  • Just with the script cited, with a datetime.strptime() – Nour Jun 18 at 12:29
  • yes, I used the InsertCursor and UpdateCursor. It's working but my problem actually is on the datetime itself – Nour Jun 18 at 12:31
  • it's too long, I'll add it in my question – Nour Jun 18 at 12:37
  • The data is a table generated by zonal statistics as table from a shapefile and a TIFF. I added some columns to use them forward (with datetime) – Nour Jun 18 at 12:42
  • 1
    The problem is your code sample. It is not reproducable and impossible to debug for anyone but you. "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body" – BERA Jun 19 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.