AnsweredAssumed Answered

Passing Parameter as Text to Field Calculator Expression

Question asked by judsoncrouch on May 10, 2016
Latest reply on May 11, 2016 by judsoncrouch

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!