Automating date calculations in field calculator

300
7
Jump to solution
06-15-2021 06:49 AM
DarrenDe_Lange
New Contributor III

Good afternoon all!

I am trying to automate date calculations using the field calculator in ArcGIS Pro.

I have defined survey dates, which I would like to use to create a reinspection dates.

The reinspection date is 1 year for high risk zones, 3 years for medium risk zone and 5 years for low risk zones.

Is there a piece of code I could use in the field calculator to automate this calculation?

The best sample I came out with for high risk zones is:

Expression:
arcpy.time.ParseDateTimeString(!SurveyDate!) + datetime.timedelta(days=365)

However, this did not work when I tried to execute the script.

Any help is appreciated.

Thank you

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Regarding

However, this did not work when I tried to execute the script.

It always helps to be more specific.  What exactly does "did not work" mean?  Did you get an error?  If so, what error, specifically?  Did you get an unexpected result?  if so, what were you expecting and what did you get?

You should be able to simply retrieve the date and add the time to it:

!SurveyDate! + datetime.timedelta(days=365)

View solution in original post

7 Replies
JoshuaBixby
MVP Esteemed Contributor

Regarding

However, this did not work when I tried to execute the script.

It always helps to be more specific.  What exactly does "did not work" mean?  Did you get an error?  If so, what error, specifically?  Did you get an unexpected result?  if so, what were you expecting and what did you get?

You should be able to simply retrieve the date and add the time to it:

!SurveyDate! + datetime.timedelta(days=365)

View solution in original post

DarrenDe_Lange
New Contributor III

Thank you for your help, that has worked. I just need to create an 'if' statement now.

Is one able to loop the statement in the field calculator whenever a new entry is created? 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Not sure what you mean by looping the statement in the field calculator. Calculate field will perform the operation on all (or selected) records once. If you want it to do multiple things when you run calculate field, you'll have to code that logic; possibly using an if statement.

If you want to automatically do something when a condition is met (new record, new value, etc), that would be something different. Maybe look into Attribute Assistant or Attribute Rules. If it's in an RDBMS, you could make a database trigger. Or if it doesn't need to be immediate, you could schedule a Python script as a Windows task.

BlakeTerhune
MVP Regular Contributor

You say it did not work but what was the result? No values? Incorrect values? Error message? In the case of an error, what was the complete message (check the geoprocessing results)?

If you're working with the datetime library, you'll need to import datetime

Try something like this for the Python code block

def calc_reinspection(survey_date):
    date_str_format = "%d/%m/%Y"
    survey_date = datetime.datetime.strptime(survey_date, date_str_format)
    reinspection_date = survey_date + datetime.timedelta(days=365)
    return datetime.datetime.strftime(reinspection_date, date_str_format)

That last line is formatting the datetime object back to a string formatted date to exclude the time portion (which defaults to midnight). You can leave that part out if you don't mind your dates all having the time displayed (even though they do have it hidden); just return reinspection_date.

And this for the Python expression:

calc_reinspection(!SurveyDate!)
DarrenDe_Lange
New Contributor III

Hi Blake - Thank you for your response. 

I am now trying to add in the risk zones and my statement does not seem to be working.

Field Calculator: 

Expression line:
reclass(!SurveyDate!)

Code block:
def reclass(SurveyDate):
if (RiskZone = LOW):
return "SurveyDate + datetime.timedelta(days=486)"
elif (RiskZone = MED):
return return "SurveyDate + datetime.timedelta(days=1095)"
elif (RiskZone = HIGH):
return return "SurveyDate + datetime.timedelta(days=1825)"


Attached is the syntax error.

Any assistance is appreciated. 

Thank you

0 Kudos
BlakeTerhune
MVP Regular Contributor

That error is because you are using a single = which is for assignment. Use double == for comparing equality. And you probably want quotes around the LOW value to indicate it's a string and not a variable that isn't defined.

 

if (RiskZone == "LOW"):

 

Also, get rid of the double return keywords; just one will do 😉

DarrenDe_Lange
New Contributor III

Thank you 😊

0 Kudos