Passing Parameter as Text to Field Calculator Expression

2710
4
Jump to solution
05-10-2016 02:11 PM
JudsonCrouch1
New Contributor III


Hello All,

I am working on creating a tool that will add or subtract time to a text field in a feature class. My thought process is that I will take the original feature class with text field "DATETIME", make a copy of the original feature class (extra step to preserve source data), add a field titled "TIME", and run a field calculator expression with user designated values in the calculator expression.

import arcpy as ap
import os
# Script Arguments
input_fc = ap.GetParameterAsText(0)
output_fc = ap.GetParameterAsText(1)
D = ap.GetParameterAsText(2)
H = ap.GetParameterAsText(3)
M = ap.GetParameterAsText(4)

# Create copy of input trajectory
ap.CopyFeatures_management(input_fc, output_fc)

# Create new field
ap.AddField_management(output_fc,"TIME", "TEXT", 50 )

# Calculate new time in new field
exp = 'arcpy.time.ParseDateTimeString(!DATETIME!) + datetime.timedelta(days=D, hours=H, minutes=M)'
ap.CalculateField_management(output_fc, "TIME", exp, "PYTHON_9.3" )

# Copy new time to DATETIME field
ap.CalculateField_management(output_fc, "DATETIME", "!TIME!", "PYTHON_9.3")

I encounter the following error:

ExecuteError: ERROR 000539: Error running expression: arcpy.time.ParseDateTimeString(u"4/28/2016 2:45:00 AM") + datetime.timedelta(days=D, hours=H, minutes=M)

Traceback (most recent call last):

  File "<expression>", line 1, in <module>

NameError: name 'D' is not defined

What gives? I know that the entire workflow process works when I do all of the steps manually but I think the "GetParamterAsText" is what is throwing me off. I am still relatively new to Python and Tool Building so its probably something pretty obvious.

Thanks in advance for the help!

0 Kudos
1 Solution

Accepted Solutions
JudsonCrouch1
New Contributor III

I figured it out. The unicode the error was referring to was the variable names (D,H,M). Instead of defining the variables ahead of time, I inserted the GetParamterAsText(x) into the corresponding argument in the timedelta expression.

# Calculate new time in new field
xp = 'arcpy.time.ParseDateTimeString(!TIME!) + datetime.timedelta(days=int(arcpy.GetParameterAsText(2)), hours=int(arcpy.GetParameterAsText(3)), minutes=int(arcpy.GetParameterAsText(4)))'

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

can you put some print statements in there before and after the exp line, I am not sure about the exp being in quotes since it isn't a code block or not concatenated until after it obtains its values.

0 Kudos
DanPatterson_Retired
MVP Emeritus

dont have arc to test

xp = 'arcpy.time.ParseDateTimeString(!DATETIME!)' + str(datetime.timedelta(days=D, hours=H, minutes=M))

JudsonCrouch1
New Contributor III

Thanks for the replies Dan.

While this eliminated the original error, I am now getting the following:

exp = "arcpy.time.ParseDateTimeString(!DATETIME!)" + str(datetime.timedelta(days=D, hours=H, minutes=M))

TypeError: unsupported type for timedelta minutes component: unicode

I did a quick scour of Google and could not find anything of use. Any idea why unicode text would throw it off?

0 Kudos
JudsonCrouch1
New Contributor III

I figured it out. The unicode the error was referring to was the variable names (D,H,M). Instead of defining the variables ahead of time, I inserted the GetParamterAsText(x) into the corresponding argument in the timedelta expression.

# Calculate new time in new field
xp = 'arcpy.time.ParseDateTimeString(!TIME!) + datetime.timedelta(days=int(arcpy.GetParameterAsText(2)), hours=int(arcpy.GetParameterAsText(3)), minutes=int(arcpy.GetParameterAsText(4)))'