1

There are droves of questions on here about handling dates/times, and I've read through volumes of them, but still don't understand what I am doing wrong.

I am trying to run a simple Python script in ArcGIS Pro but keep encountering errors... mostly having to do with data type conflicts. What I want to do is look through a field in a feature class table to find the most-recent (max) date, and then compare that with another variable date.

Here is what I have:

import arcpy
import datetime
import time

arcpy.env.workspace = "C:\Data\Reporting.gdb"

ptFC = "C:\Data\Reporting.gdb\RMI_pts"
listDates = []

cursor = arcpy.da.SearchCursor(ptFC, "ReportQueryDate")

for row in cursor:
    listDates.append(row)

maxDateTuple = max(listDates)
maxDateDT = datetime.datetime(maxDateTuple[0:6])

if maxDateDT < datetime.datetime.today():
    print (maxDateTuple)
else:
    print ("doh!")

The code above returns an error on line 18 stating that in integer is required but it got a tuple. The actual error is:

Traceback (most recent call last): File "", line 16, in TypeError: an integer is required (got type tuple)

I don't know if my approach is correct: What is the best way to return a 'max' date from the table and then compare it to today's date?

2
  • 1
    What is the precise and full error message that you received when running the code that you have presented?
    – PolyGeo
    Dec 12, 2019 at 22:28
  • 1
    Cursors ALWAYS return a sequence, even if you've only requested a single field. So listDates is currently a list of single-item tuples
    – mikewatt
    Dec 12, 2019 at 22:29

2 Answers 2

1

Get the first element of the tuple as mentioned here: https://stackoverflow.com/questions/32211300/tuple-to-datetime-object

The below mentioned code works for me!


import arcpy
import datetime
import time
arcpy.env.workspace = r"D:\Data\Alisha\TemplateData\TemplateData\Test\New File Geodatabase.gdb"
ptFC = r"D:\Data\Alisha\TemplateData\TemplateData\Test\New File Geodatabase.gdb\states"
listDates = []
cursor = arcpy.da.SearchCursor(ptFC, "Testd")
for row in cursor:
    listDates.append(row)
maxDateTuple = max(listDates)
if maxDateTuple[0]<datetime.datetime.today():
    print(maxDateTuple)
else: 
    print "doh"

4
  • 2
    listDates.append(row[0]) would be better.
    – user2856
    Dec 13, 2019 at 1:38
  • 1
    Thanks @Alisha, your answer solved my problem. I think I don't understand my list of tuples correctly. I thought the first item in the tuple was just the year. Dec 13, 2019 at 14:34
  • 1
    One note about the code above is that if in Pro the print "doh" should be print( "doh") since it Pro is using python 3.
    – dark fader
    Dec 13, 2019 at 19:53
  • Also, "doh!" must always have an exclamation point...not for proper python syntax, but for proper "Homer Simpson" syntax. Dec 14, 2019 at 19:41
1

As mikewatt noted in comment the SearchCursor returns a tuple with an entry for each field requested.

In your case you only requested a single field ("ReportQueryDate") so you get a tuple with a single item, and that item is of type datetime. So if you print it the row in the for row in cursor: loop you'd see something like: (datetime.datetime(2019, 12, 11, 0, 0),). So note it is (value,), that is the first item is datetime.datetime(2019, 12, 11, 0, 0) and there is no second item.

If you asked for multiple field, like say cursor = arcpy.da.SearchCursor(ptFC, ["ReportQueryDate","OBJECTID"]) then you'd get back: (datetime.datetime(2019, 12, 11, 0, 0), 1). In this case it is (value,value). That is the first item is datetime.datetime(2019, 12, 11, 0, 0) and the second item is 1.

Also since you mentioned "I thought the first item in the tuple was just the year" the code is not just comparing the year it is comparing datetime objects so it is comparing the year, month, day and time.

The example below makes a list of datetime objects, not the entire tuple (as user2856 suggested). It also checks for cases where field value are NULL, and if you only want to compare the year.

import arcpy
import datetime
import time

#Note for paths you need to prefix with "r" or you need to use \\ or /
arcpy.env.workspace = r"C:\Data\Reporting.gdb"
ptFC = r"C:\Data\Reporting.gdb\RMI_pts"


listDates = []


cursor = arcpy.da.SearchCursor(ptFC, ["ReportQueryDate"])
for row in cursor:        
    # since field could be NULL check if there is value
    if row[0]!=None:
        #add just the first item from the tuple to the list
        #so list will contain datetime objects
        listDates.append(row[0])


# check if list is empty (if all dates were null)
if not listDates:
    print ("List is empty")
else:    
    maxDate = max(listDates)    

    #this is comparing entire objects  
    if maxDate < datetime.datetime.today():
        print(maxDate)
    else: 
        print("Doh ... max date found " + str(maxDate))        

    #if you just want to compare year you can get year from object
    if maxDate.year < datetime.datetime.today().year:
        print(maxDate.year)
    else: 
        print("Doh ... max year found " + str(maxDate.year ))  
1
  • Thanks @dark fader. This is just the type of explanation I needed. I had a sneaking suspicion that I was misunderstanding exactly the datetime tuple, but now it's much clearer to me. Thanks again- Dec 14, 2019 at 19:43

Your Answer

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

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