Select to view content in your preferred language

# Day of the year calculation in ArcPy syntax help

771
3
03-28-2019 08:08 AM
Occasional Contributor

I am trying to calculate a "day of the year" field in a feature class using ArcPy.

``````###create day update cursor
cursorD = arcpy.da.UpdateCursor(featureclass,["month", "day", "dayoftheyear"])
print("day cursor set")

###loop and assign day of the year
for updateRowD in cursorD:
if 'month' == 1:
print("Jan")
"dayoftheyear" = [day]
cursorD.updateRow(updateRowD)
print("Jan completed")
elif 'month' == 2:
print("Feb")
"dayoftheyear" = [day] + 31
cursorD.updateRow(updateRowD)
print("Feb completed")
elif 'month' == 3:
print("Mar")
"dayoftheyear" = [day] + 59
cursorD.updateRow(updateRowD)
print("Mar completed")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

and continues throughout the rest of the year (April through December). I'm getting syntax errors on the lines where I actually calculate the day number (lines 9, 14, 19, etc). I just can't figure out the syntax to get it to read the value of a field (day), perform basic arithmetic on it, and assign it to another field (dayoftheyear). I've used lots of combinations of [day], "day", !day!, etc and haven't been able to find my answer in any documentation.

Tags (3)
1 Solution

Accepted Solutions
MVP Honored Contributor

Save yourself the heartache of dealing with dates semi-manually by relying on the datetime module, which will handle it in one line and deal with leap years:

``````from datetime import datetime

year = 2020
month = 2
day = 29

day_of_year = datetime(year, month, day).timetuple().tm_yday
print(day_of_year)

60‍‍‍‍‍‍‍‍‍‍``````
3 Replies
by
Frequent Contributor

I think the proper syntax is to access the fields using their indexes, as ordered in your fields list. For example:

``````cursorD = arcpy.da.UpdateCursor(featureclass,["month", "day", "dayoftheyear"])

for updateRowD in cursorD:
if updateRowD[0] == 1:
updateRowD[2] = updateRowD[1]
elif updateRowD[0] == 2:
updateRowD[2] = updateRowD[1] + 31
elif updateRowD[0] == 3:
updateRowD[2] = updateRowD[1] + 59

cursorD.updateRow(updateRowD)

del updateRowD
del cursorD‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

The del statements are used to clear potential locks on the targeted feature class. You might consider creating the cursor using a with statement so that locks are cleared automatically:

``````with arcpy.da.UpdateCursor(featureclass,["month", "day", "dayoftheyear"]) as cursorD:‍
for updateRowD in cursorD:‍‍‍‍‍‍
if updateRowD[0] == 1:
updateRowD[2] = updateRowD[1]
elif updateRowD[0] == 2:
updateRowD[2] = updateRowD[1] + 31
elif updateRowD[0] == 3:
updateRowD[2] = updateRowD[1] + 59

cursorD.updateRow(updateRowD)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

You may also want to revise your print statements, since they'll print at every row.

MVP Esteemed Contributor

I suggest you read Accessing data using cursors—Help | ArcGIS Desktop  , it will help you understand how to work with cursors.  Second, are you three fields just numeric/integer?

MVP Honored Contributor

Save yourself the heartache of dealing with dates semi-manually by relying on the datetime module, which will handle it in one line and deal with leap years:

``````from datetime import datetime

year = 2020
month = 2
day = 29

day_of_year = datetime(year, month, day).timetuple().tm_yday
print(day_of_year)

60‍‍‍‍‍‍‍‍‍‍``````