The following code runs fine minus receiving the error below, which doesn't make any sense (to me) as I have other UpdateCursors
and perform UpdateRow(row)
with other date values. As can be seen below, there are other instances in which I update a row using a "DATE" value, but for some reason this one is throwing an error.
Error:
…line 79, in cursor.updateRow(row) TypeError: value #0 –
unsupported type: datetime.timedelta
Code that receives error:
# UpdateCursor that will write the time difference calculation to the new Total_Time field
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Time")
for row in cursor:
row[0] = timeDiff
cursor.updateRow(row)
Entire code:
# import arcpy module
import arcpy
# Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)
# Set workspace environment based upon user's choice
arcpy.env.workspace = userWorkspace
# Ask user to select an Oracle dB Connection
oracleDB = arcpy.GetParameterAsText(1)
# Ask user to name the Query Layer
queryLayer = arcpy.GetParameterAsText(2)
# Ask user for an SQL Query Expression to be run against the selected Oracle dB
sqlQuery = arcpy.GetParameterAsText(3)
# Create spatial reference variable to assign to queryLayer
spatialRef = arcpy.SpatialReference("W:\Coordinate Systems\WGS 1984.prj")
# Process: 'Make Query Layer' - Creates a Query Layer using the user's Oracle dB and SQL query expression
arcpy.MakeQueryLayer_management(oracleDB, "Temp_Layer", sqlQuery, "UNIQUE_ID", "POLYLINE", "1050010", spatialRef)
# Process: 'Copy Features' - Copies the temporary Query Layer and stores it as a permanent feature class
arcpy.CopyFeatures_management("Temp_Layer", queryLayer)
# Process: 'Define Projection' - Defines the projection of queryLayer feature class output
arcpy.DefineProjection_management(queryLayer, spatialRef)
# Process: 'Add Field' - Adds new column fields to queryLayer
arcpy.AddField_management(queryLayer, "First_Time", "DATE") # The first LOGDT ping
arcpy.AddField_management(queryLayer, "Last_Time", "DATE") # The last LOGDT ping
arcpy.AddField_management(queryLayer, "Total_Time", "DATE") # Summation of the first to last ping in time
arcpy.AddField_management(queryLayer, "Total_Pings", "INTEGER") # Total number of pings (rows)
arcpy.AddField_management(queryLayer, "Perfect_Pings", "INTEGER") # Total number of pings possible in given timeframe
arcpy.AddField_management(queryLayer, "Time_to_Process", "DATE") # How long it took for each ping to process
# Calculates the total number of rows for newly added column Total_Pings
numRows = int(arcpy.GetCount_management(queryLayer).getOutput(0))
# UpdateCursor that will write the value of numRows to the Total_Pings column
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Pings")
for row in cursor:
row[0] = numRows
cursor.updateRow(row)
# SearchCursor that will read the values of LOGDT and return the first value (earliest time)
firstLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][0]
# UpdateCursor that will write the first (earliest time) LOGDT value to the field First_Time
cursor = arcpy.da.UpdateCursor(queryLayer, "First_Time")
for row in cursor:
row[0] = firstLogdt
cursor.updateRow(row)
# SearchCursor that will read the values of LOGDT and return the last value (latest time)
lastLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][-1]
# UpdateCursor that will write the last (latest time) LOGDT value to the field Last_Time
cursor = arcpy.da.UpdateCursor(queryLayer, "Last_Time")
for row in cursor:
row[0] = lastLogdt
cursor.updateRow(row)
# Calculates the difference between firstLogdt and lastLogdt
timeDiff = lastLogdt - firstLogdt
# UpdateCursor that will write the time difference calculation to the new Total_Time field
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Time")
for row in cursor:
row[0] = timeDiff
cursor.updateRow(row)
# Calculates the total number of pings that would occur if no pings were dropped
perfectPings = timeDiff.total_seconds() / 5 # 1 ping every 5 seconds (with good reception)
# UpdateCursor that will write the total number of pings possible to the new Perfect_Pings field
cursor = arcpy.da.UpdateCursor(queryLayer, "Perfect_Pings")
for row in cursor:
row[0] = perfectPings
cursor.updateRow(row)
EDIT: As suggested, trying to cast the variable timeDiff as a string does not work and returns this:
RuntimeError: The value type is incompatible with the field type. [Total_Time]
Best Answer
This line here creates a timedelta object:
A datetime object is different type than a timedelta. The timedelta object doesn't fit with a date field (hence the TypeError).
You could instead create a double field instead of a date field, and store the difference in seconds (or do a little extra math if you wanted minutes, hours, etc.)