0

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]

  • @BenSNadler - I thought the same thing, but that wouldn't make any sense as I have other instances of this that run just fine. I did try it anyhow and received this new error: RuntimeError: The value type is incompatible with the field type. [Total_Time] – JumpInTheFire Jun 17 '15 at 20:47
4

This line here creates a timedelta object:

# Calculates the difference between firstLogdt and lastLogdt
timeDiff = lastLogdt - firstLogdt

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.)

for row in cursor:
    row[0] = timeDiff.total_seconds()
    cursor.updateRow(row)
  • 1
    +1 @JumpInTheFire is trying to update a field that is expecting <type 'datetime.datetime'> object with a <type 'datetime.timedelta'> object. – user2856 Jun 17 '15 at 22:49
  • Thank you both for your help. I was previously unaware of the two distinct differences (datetime and timedelta). Now I'm curious as to why a datetime value with some sort of arithmetic involved cannot output another datetime value? – JumpInTheFire Jun 18 '15 at 12:45
  • 1
    @JumpInTheFire. Check out PyMOTW, which as always gives a good walkthrough: pymotw.com/2/datetime – DWynne Jun 18 '15 at 18:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.