Nested If statements for applying text data to field

07-09-2019 07:45 AM
New Contributor III


Based on my dataset, I have to apply a flag into a text field.  This flag is derived by first looking at an ID field called SID (text), and determining the first two characters of it. Based on those 2 characters, it then looks at a date field called FullDateTime, and assigns the flag value if the time in FullDateTime falls within a specific range. Problem is, my code is not accepted by the field calculator in ArcPro 2.4:

AttendanceFlag =

DetermineAttendance (!SchoolAttendance!)

from datetime import datetime


def DetermineAttendance(SchoolAttendance):

SchoolID = SID[:2]  # variable to examine leftmost 2 characters in the SID field


If DayType == “Weekend”:

                                SchoolAttendance = “Weekend”


                                If SchoolID == “RL”:

                                                If FullDateTime.time() > = 8:55 AM and FullDateTime.time() <= 3:30 PM:

                                                                SchoolAttendance = “InSchool”


                                                                SchoolAttendance = “OutSchool”


                                                If FullDateTime.time() > = 9:10 AM and FullDateTime.time() <= 3:45 PM:

                                                                SchoolAttendance = “InSchool”


                                                                SchoolAttendance = “OutSchool”

This is my fields setup (DaysSeconds has been changed to a text field called AttendanceFlag:

0 Kudos
1 Reply
MVP Regular Contributor


Not sure if you still need help on this one.  Lets to start with a few larger issues and we can work from there.

1) when calling and defining a function you need to include the parameters from your source that you will be consuming.  In this case it looks like you need the fields SID, DayType, FullDateTime, etc.  This should not be the name of the attribute being returned, as you have it defined.  For example your call to the function should be

DetermineAttendance(!SID!, !DayType!, !FullDateTime!)

In your def statement you define the variable names that will be used within the method corresponding to the ones passed.  Such as below using the same field names SID, DayType and FullDateTime

def DetermineAttendance(SID, DayType, FullDateTime):

Or you can simply the names in the def statement such as use s, do and fd and you would use these variables in the  function.

def DetermineAttendance(s, dt, fd):

2) you are using the time() method of a datetime to compare a time value “FullDateTime.time() > = 8:55 AM

The time() method returns a string along the format of 12:15:20.123456.  If you are comparing this to a string “8:55 AM” (which needs to have quotes around it) the chance of it matching is not going to happen. You would need to format the time returned using strftime() to get a match.  Experiment a little using a python command line to see how this works.  If you need more help, just post your updated code.

3) you need a return statement at the end of your code to send the value of SchoolAttendance back to the call.

return SchoolAttendance

4) on a couple of your if statements you have “> =“, there should not be a space between these characters.

0 Kudos