So, I'm guessing the relativedelta function just doesn't support a variable substitution for the integer part. I'm trying to substitute a user enter numeric parameter into the function and get the following error. Does anyone know if there is a way to work around this using another function?
https://dateutil.readthedocs.io/en/stable/relativedelta.html
FYI, I'm trying to find all the assets in a feature class that have been sold during a user enter time period from todays date. e.g. if I wanted to find all the assets that have been sold within the past year I would enter 12 for the number of months.
Also, note the script works fine when I hard code in a numeric value for numMonths into the function.
Thank you for any help,
Mark
Traceback (most recent call last):
File "D:\GIS_Data\Contractors\Mark\Vetting Tools\FindParcelsSoldInPast.py", line 26, in <module>
timesPast = (datetime.datetime.now() - relativedelta(months=numMonths)).strftime("%Y-%m-%d")
File "C:\Python27\ArcGIS10.8\lib\site-packages\dateutil\relativedelta.py", line 127, in __init__
raise ValueError("Non-integer years and months are "
ValueError: Non-integer years and months are ambiguous and not currently supported.
# Import arcpy module
import arcpy
import datetime
from dateutil.relativedelta import relativedelta
## Parameters
workspace = arcpy.GetParameterAsText(0) #As type Workspace
getFeatureLayer = arcpy.GetParameterAsText(1) #As type feature layer
dateSoldField = arcpy.GetParameterAsText(2) #As type field
numMonths = arcpy.GetParameterAsText(3) #As type Long
## Variables
timesPast = (datetime.datetime.now() - relativedelta(months=numMonths)).strftime("%Y-%m-%d")
SQL = "\"{}\" >= date \'{}\'".format(dateSoldField, timesPast)
## Geoprocessing:
arcpy.SelectLayerByAttribute_management(getFeatureLayer, "NEW_SELECTION", SQL)
Solved! Go to Solution.
numMonths = arcpy.GetParameterAsText(3)
You're inputting a string into relativeDelta. Try these:
numMonths = arcpy.GetParameter(3)
numMonths = int(arcpy.GetParameterAsText(3))
numMonths = arcpy.GetParameterAsText(3)
You're inputting a string into relativeDelta. Try these:
numMonths = arcpy.GetParameter(3)
numMonths = int(arcpy.GetParameterAsText(3))
🤣LOL, Thank you @JohannesLindner I thought that since I set the variable in the script parameters to be a type "Long" that it would already be an integer value.