-2

I am trying to calculate (using Arcpy) the time difference between two fields that are in string data type and they have the following time format "YYY-MM-DD HH:mm:SS.sss"

I have achieved that between two fields with data type "Datetime" with the following script,

import arcpy
from arcpy import da
import datetime
in_table = arcpy.GetParameterAsText(0)
out_table = arcpy.GetParameterAsText(1)
#define table into dictionary
tf = datetime.datetime.strptime([row[0] for row in arcpy.da.SearchCursor(in_table, ["TimeFinal"])], "%Y-%m%-%d %H:%M:%S.%f")
ti = datetime.datetime.strptime([row[0] for row in arcpy.da.SearchCursor(in_table, ["TimeInitial"])], "%Y-%m%-%d %H:%M:%S.%f")
#set firstRow variable to zero
counter = 1
index = 0
with arcpy.da.UpdateCursor(in_table, ["TimeDiff"]) as cursor:
     for row in cursor:
         if index<=len(ti)-2:
         n0 = ti[index]
         n1 = tf[index+1]
         if n1 >= n0:
             row[0] = int((n1 - n0).total_seconds())
         else:
             counter+=1
         cursor.updateRow(row)
         index+=1
         else:
         cursor.updateRow(row)
         break
with arcpy.da.UpdateCursor(in_table, ["TimeDiff"]) as cursor:
      for row in cursor:
          if row[0] == None:
              cursor.deleteRow()
          else:
              cursor.updateRow(row)
arcpy.CopyRows_management(in_table, out_table)

When I try to read the fields using update cursor using datetime.strptime the script send the error "strptime() argument 1 must be str, not list", does anyone know another approach to calculate the time difference in seconds or microseconds?

1
  • Thank you so much for your observation. I have included the datetime.datetime.strptime in the script in a way to try to read through it to made to calculate the time difference between the two fields.
    – cerroGIS
    Aug 16, 2022 at 2:03

1 Answer 1

1

This line...

datetime.datetime.strptime([row[0] for row in arcpy.da.SearchCursor(in_table, ["TimeFinal"])], "%Y-%m%-%d %H:%M:%S.%f")

...is invalid (as is the similar line after it). As the error says, you are passing a list as the first argument to datetime.datetime.strptime() which expects a single string for the first argument.

To see what you are actually passing as the first argument, add this line just before it:

print([row[0] for row in arcpy.da.SearchCursor(in_table, ["TimeFinal"])

It will print a list with one value for each record in the database cursor. You cannot pass this list to datetime.datetime.strptime(). It needs a single string.

If you are wanting to get a list of dates for each of those two lists of strings, you could do it like this:

tf = [datetime.datetime.strptime(row[0], "%Y-%m%-%d %H:%M:%S.%f") for row in arcpy.da.SearchCursor(in_table, ["TimeFinal"])]
ti = [datetime.datetime.strptime(row[0], "%Y-%m%-%d %H:%M:%S.%f") for row in arcpy.da.SearchCursor(in_table, ["TimeInitial"])]

Do a web search for python list comprehension to read some explanations of how the 'list comprehension' syntax works in Python.

1
  • Masterclass!, thank you so much! it worked!
    – cerroGIS
    Aug 16, 2022 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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