2

I'm trying to populate a field (Season, "TEXT") in my attribute table with a season code based on a sampling date (FIXDATE, "DATE"). For example sample collected between 01/01/2016 and 01/03/2016 would get season code "SPRING". I'm new to arcpy and search cursors. Here is my code so far.

import arcpy
from datetime import datetime

caribou = "C:/Python/Test_data.gdb/test_data"
fields = ['FIXDATE', 'Season']
with arcpy.da.UpdateCursor(caribou, fields) as cursor:
    for row in cursor:
        if (row[0] >= datetime.strptime("01/01/2016", "%d/%m/%y") and row[0] <= datetime.strptime("01/03/2016", "%d/%m/%y")):
            row[1] = "SPRING"
        elif (row[0] >= datetime.strptime("02/03/2016", "%d/%m/%y")and row[0] <= datetime.strptime("01/06/2016", "%d/%m/%y")):
            row[1] = "SUMMER"
        elif (row[0] >= datetime.strptime("02/06/2016", "%d/%m/%y") and row[0] <= datetime.strptime("01/10/2016", "%d/%m/%y")):
            row[1] = "FALL"
        elif (row[0] >= datetime.strptime("02/10/2016", "%d/%m/%y")):
            row[1] = "WINTER"

    # Update the cursor with the updated list
        cursor.updateRow(row)
3
  • 1
    What's happening so far with your result? Not sure if this was a copy/paste error, but you need to indent your cursor loop (for row in cursor... inside the with statement.
    – Evan
    Aug 31, 2016 at 17:57
  • What happens when you run this? Are there errors or just a result you're not expecting/wanting?
    – Midavalo
    Aug 31, 2016 at 18:11
  • Thanks. Yes I have been getting this error message: File "C:\Python27\ArcGIS10.1\lib_strptime.py", line 328, in _strptime data_string[found.end():]) ValueError: unconverted data remains: 16
    – rkite
    Aug 31, 2016 at 19:30

1 Answer 1

1

You need to convert row[0] to the same format as your datetime.strptime("02/06/2016", "%d/%m/%y"), and also your %y needs to be %Y for the 4 digit year. I have added an extra line myDate = datetime.strptime(datetime.strftime(row[0], "%d/%m/%Y"), "%d/%m/%Y") to convert the row[0] and then use that in each if/elif for comparison.

import arcpy
from datetime import datetime

caribou = "C:/Python/Test_data.gdb/test_data"
fields = ['FIXDATE', 'Season']

with arcpy.da.UpdateCursor(caribou, fields) as cursor:
    for row in cursor:
        myDate = datetime.strptime(datetime.strftime(row[0], "%d/%m/%Y"), "%d/%m/%Y")
        if (myDate >= datetime.strptime("01/01/2016", "%d/%m/%Y") and myDate <= datetime.strptime("01/03/2016", "%d/%m/%Y")):
            row[1] = "SPRING"
        elif (myDate >= datetime.strptime("02/03/2016", "%d/%m/%Y")and myDate <= datetime.strptime("01/06/2016", "%d/%m/%Y")):
            row[1] = "SUMMER"
        elif (myDate >= datetime.strptime("02/06/2016", "%d/%m/%Y") and myDate <= datetime.strptime("01/10/2016", "%d/%m/%Y")):
            row[1] = "FALL"
        elif (myDate >= datetime.strptime("02/10/2016", "%d/%m/%Y")):
            row[1] = "WINTER"

    # Update the cursor with the updated list
        cursor.updateRow(row)
0

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.