4

I have data that was collected over many years and I'd like to be able to run a python script tool in ArcToolbox (ArcGIS 10.1) to get a list of unique years from the date field.

I found an example of how to format the date field, but this example lists the year from every record, and I just want a list of unique years.

# Imports
import arcpy
from datetime import datetime

# Input Table
fc = arcpy.GetParameterAsText(0)

# Date Field
field = arcpy.GetParameterAsText(1)

rows = arcpy.SearchCursor(fc)
for row in rows:
    datetimeVal = row.getValue(field)
    formattedTime = datetime.strftime(datetimeVal, "%Y")
    arcpy.AddMessage(formattedTime)

I can get a list of unique dates using a search cursor, but I just want the year and not the entire date field:

# imports
import arcpy
from datetime import datetime

# Input Table
inputTable = arcpy.GetParameterAsText(0)

# Date Field
field = arcpy.GetParameterAsText(1)

# Use SearchCursor with list comprehension to return a
#  unique set of values in the specified field
values = [row[0] for row in arcpy.da.SearchCursor(inputTable, (field))]
uniqueValues = set(values)
arcpy.AddMessage(uniqueValues)

How can I get the date in the example above, format it to just get the year, and then run the search cursor to get a list of unique years?

3
  • 2
    if you don't specifically need to use python, you can just add a field to your data, calculate that field to be the year from the date field, and then run the frequency tool using the year field. the resulting table will have each unique year, and a count of how many times it occurs.
    – Adam
    Commented Feb 23, 2016 at 0:13
  • Thanks @Adam, That's what I would normally do, but in this case I specially need to add this to an existing python script.
    – Kara
    Commented Feb 23, 2016 at 16:29
  • that's cool. the process I described could be implemented as part of a script.
    – Adam
    Commented Feb 23, 2016 at 23:24

3 Answers 3

5

Here is one approach that uses the datetime module to parse the year from a datetime object. The SearchCursor is wrapped in a generator expression to extract unique years from the "date" field in the table.

import arcpy
from datetime import date

table = r'C:\path\to\your.gdb\table'

print set(date.strftime(row[0], "%Y") for row in arcpy.da.SearchCursor(table, "date"))

enter image description here

Result:

>>> 
set(['2008', '2009', '2011', '2022'])
0
3

By combining parts from the two different python scripts you've shown, you can accomplish what you're after.

# Imports
import arcpy
from datetime import datetime

# Input Table
fc = arcpy.GetParameterAsText(0)

# Date Field
field = arcpy.GetParameterAsText(1)

# Search Cursor
rows = arcpy.da.SearchCursor(fc,field)

# Create an empty list to fill with years
allYears = []

for row in rows:
    datetimeVal = row[0]
    formattedTime = datetime.strftime(datetimeVal, "%Y")
    # add the current row's year to the list
    allYears.append(formattedTime)

#create a unique set of the years
uniqueYears = [ year for year in set(allYears) ]
# sort years
uniqueYears.sort()

uniqueYears is a list that will contain a unique set of years from your input table. If your input table has Null values in the date field you'll need to test for those or you'll raise a TypeError.

1
  • Thanks @nwduncan, that's exactly what I was looking for! All my attempts and combining parts from the two different python scripts were failing.
    – Kara
    Commented Feb 23, 2016 at 16:21
0

I agree with Adam. Another option though if you have access to your data in a standard database, would be use a combination of datepart and group by. something similar to:

select datepart('yyyy',[your_date_field])
from [your_data_set]
group by datepart('yyyy',[your_date_field])

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.