Select to view content in your preferred language

Python Error

3171
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
1 Solution

Accepted Solutions
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)

 

View solution in original post

13 Replies
Kara_Shindle
Regular Contributor

Your model looks like ArcPro, but the instructions are for ArcGIS Desktop.  Are you sure that is the correct tool to call in Pro?

 

I did see this article - looks like that tool may be depreciated at Pro 3.0.  You might have to do this in ArcMap if you want to finish your project.

https://support.esri.com/en/technical-article/000027752

StephenSmith8847
New Contributor III

That looks to be the issue then! Is there a way around this, to use python to achieve the same results?

0 Kudos
MichaelVolz
Esteemed Contributor

Have you tried exporting the model out to python and just working on the geoprocess in python instead of ModelBuilder?

What was the geoprocess that was deprecated at Pro 3.0?  Do you know what similar geoprocess would perform the same task in Pro 3.0, if indeed the geoprocess called in the sample you are starting from has been deprecated?

Kara_Shindle
Regular Contributor

It looks like the support article suggests a few alternatives.  Personally, I prefer the datetime module, which appears to already be used in your expression.

I did just notice that the screenshots in your instructions are from Pro even though this is listed under Desktop.  They might update the instructions soon.  

 

Looks like that expression starts to parse a date string for an attribute in your dataset.  There are ways to parse a string to a datetime, such as using the strptime() method, which is a built-in method of the datetime class.  Try something like this: 

datetime.strptime(date_string, format)

 I use something similar in a database reconciliation script:

dateToday = currentTime.strftime("%Y-%m-%d")
StephenSmith8847
New Contributor III

Appreciate your time and help on this, but i'm still struggling. How does it know which data table (which is added to my project) and then which field within that data table to run the expression against?

All we are trying to do in this expression is i believe allow us to select any date. Next in the model is a SQL expression which i beleive then retrieves 365 days from that date. 

 

StephenSmith8847_0-1665409258715.png

 

It's giving me an error, is this correct?

Thanks again, i appreciate your patience.

 

0 Kudos
Kara_Shindle
Regular Contributor

Edit:  

I updated my answer to just have a replacement for the arcpy.time.ParseDateTimeString('ReportDate%') with an alternative as a part of the datetime module.

This calculation is just creating an expression that accepts any string formatted like "month/day/year" i.e. "10/10/2022"

Building this expression means that you will be able to input that date when you run the actual tool, and the date will be consumed in the "Select By Layer Attribute" tool.

 

For a simpler expression, you can also try the following:

 

 

 

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

 

 

Kara_Shindle_1-1665414937100.png

 

 

StephenSmith8847
New Contributor III

Hi,

 

I've just copied your expression exactly: 

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

 

and i get this still: 

Executing (Calculate Value): CalculateValue "datetime.datetime.strptime('01/04/2021 01:00:00', '%m/%d/%Y') - datetime.timedelta(days = 365)" # Date
Start Time: 10 October 2022 16:26:57
ERROR 000539: Traceback (most recent call last):
  File "<expression>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
Failed to execute (Calculate Value).
Failed at 10 October 2022 16:26:57 (Elapsed Time: 0.01 seconds)

what am i doing wrong?

 

0 Kudos
Brian_Wilson
Regular Contributor II

Just a style comment for you -- I usually use a separate code block, like this,

BrianWilson7_1-1665417311635.png

(I have no idea if the date compare "today > edit" would work - this is just to show the code block.)

That way I can test the code in a separate notebook, I can have import statements at the top, and I can spread the code out over several lines so it's easier to read and understand.

 

RachelGomez
New Contributor II

The correct way to fix a python error


Read the error from the beginning. The first line tells you the location of the error. 
Next, look at the error type. In this case, the error type is a SyntaxError. 
Look at the details of the error. 
Time to use your logic.

 

Regards,

Rachel Gomez

0 Kudos