Select to view content in your preferred language

Python Error

3169
13
Jump to solution
10-10-2022 05:10 AM
StephenSmith8847
New Contributor III

Hi,

 

I am trying to follow this guide: Analyzing violent crime automation—Analytics | Documentation (arcgis.com)

I've followed it successfully, using my own data and I am at the stage of building the model. 

StephenSmith8847_0-1665403409975.png

 

I first created a varialbe for the report data which will be used to select any date, i've made it a peramter and named it Date (the first oval), i have then added calculate value and attempted to add the following expression:

arcpy.time.ParseDateTimeString('%ReportDate%') - datetime.timedelta(days = 365)

 

I then changed it to this to match my field names for where the date is: 

arcpy.time.ParseDateTimeString('%Created_Date%') - datetime.timedelta(days = 365)

 

I'm getting an error:

ERROR 000539: Traceback (most recent call last):
File "<expression>", line 1, in <module>
AttributeError: module 'arcpy' has no attribute 'time'
Failed to execute (Calculate Value).

I'm using ArcGIS Pro version 3

Any help would be greatly appreciated.

 

 

0 Kudos
13 Replies
StephenSmith8847
New Contributor III

Ok thanks i'll try stlying like that next time, but i copied and pasted the previous posts expression and it didn't work but I can see it has worked for them?

0 Kudos
Kara_Shindle
Regular Contributor

We are working on different Python versions - I have not updated to 3.0. so you might have different errors then I see.

You might try removing the first datetime in the expression, as the first part has it twice and the 2nd one has it once. i.e. like this

 

datetime.strptime('%ReportDate%', '%m/%d/%Y') - datetime.timedelta(days = 365)

 

When I tested in Python, I was able to create a definition formated similar to what @Brian_Wilson  posted.

 

import datetime

string = '10/10/2022'

def startTime(string):
	date = datetime.datetime.strptime(string, '%m/%d/%Y')
	print(date)
	finalDate = date - datetime.timedelta(days = 365)
	print(finalDate)
	return finalDate

startTime(string)

 

Brian_Wilson
Regular Contributor II

Try just datetime.strptime instead of datetime.datetime.strptime

 

StephenSmith8847
New Contributor III

Thank you all so much. It's worked! I went into the ReportDate parameter, removed the hh:mm:ss leaving just the date, and after seeing your python layout i noticed it was a minus symbol, but i wanted a + symbol so as you can see below i changed it to + date.timedelta(days=365) and it worked.... would this have been the reason it wasn't working for some reason? 

 

To confirm, the final working expression is as follows: 

datetime.datetime.strptime('%ReportDate%', '%d/%m/%Y') + datetime.timedelta(days = 365)

 

This is now what i get when i run it.

Executing (Calculate Value): CalculateValue "datetime.datetime.strptime('01/01/2020', '%d/%m/%Y') + datetime.timedelta(days = 365)" # Date
Start Time: 10 October 2022 20:56:11
Value = 31/12/2020
Succeeded at 10 October 2022 20:56:11 (Elapsed Time: 0.01 seconds)

Thanks again everyone for your time in helping me, it's very much appreciated!

0 Kudos